🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

trying to learn arrays in C#, can't make a method that stops scrolling the array's index if it's not lower than the array's length.

Started by
6 comments, last by SassyPantsy 3 years, 10 months ago

so i've constructed an exercise for myself to create an array that scrolls between it's values whenever the user presses space. it took my about 4 hours of trial and error but i've finally managed to make it work. now i'm trying to make the scrolling method not execute if the arrays index goes out of bounds, but i can't seem make it work.

i've tried many things, one of which was to put a condition inside the input if statement that adds that it will only work if the index is (which is an int called currentText) is < text.Length. this was the if statement's condition:

if (Input.GetKeyDown (KeyCode.Space) && currentText < text.Length){

ScrollText(); //which is a method i've created.

}

else if ( Input.GetKeyDown (KeyCode.Space) && currentText ≥ text.Length){

gameObject.SetActive(false);

this was the original code. i've changed it to be as follows, but either way the else if / else statements don't get executed and i recieve an out of bounds error. this is the current code, thanks:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DialogueManager : MonoBehaviour
{
    public GameObject[] text = new GameObject[3];

    int currentText = 0;

    void Start()
    {
        text[currentText].SetActive(true);
    }

    void ScrollText()
    {
		if (currentText < text.Length)
		{
            text[currentText].SetActive(false);
            currentText++;
            text[currentText].SetActive(true);
        }
		else
		{
            gameObject.SetActive(false);
        }
        
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
		{
            ScrollText();
        }
    }
}

  

None

Advertisement

Try

currentText = (currentText + 1) % currentText.Count();

Instead of

currentText++;

SuperVGA said:

Try

currentText = (currentText + 1) % currentText.Count();

Instead of

currentText++;

What will it do differently?

None

If you want the method to not scroll down when you are at the last item, you want

if (currentText < text.Length-1)

Otherwise if currentText equals text.Length-1 then when you increment currentText executing

text[currentText].setActive(true);

will call setActive on text[text.Length] which is out of bounds. From a logical perspective it also makes sense to block scrolling down if you are at the last item (which is what my modification does).

SassyPantsy said:

SuperVGA said:

Try

currentText = (currentText + 1) % currentText.Count();

Instead of

currentText++;

What will it do differently?

It will loop currentText back to 0 instead of hitting the element just out of bounds.

% is the modulo operator.

@RulerOfNothing worked! i think i understand the mathematical logic, i guess numbers confuse me more than i thought.

None

@SuperVGA the % operator is still something i'm trying to figure out. this thread was mostly about trying to figure out why the if (currentText<Text.Length) isn't working as intended.

None

This topic is closed to new replies.

Advertisement