I have a problem related to arrays and it’s indexes.
The code:
public string [] myName = new string[5];
public Texture2D [] myTexture = new Texture2D[5];
private string curNameString;
private Texture2D curNameTexture;
// Initialization
void Start () {
agent = GetComponent<NavMeshAgent> ();
curNameString = myName[Random.Range(0, myName.Length)];
curNameTexture = myTexture[Random.Range(0, myTexture.Length)];
}
I am trying to make the index of curNameTexture equal to the index of the myName array.
Look - we use myName to choose a random name of our array. Works fine.
Each person has also a picture (Texture2D) attached to that specific array.
So if I would do the following:
…it doesn’t stay equal to the random chosen number of the myName-array.
What am I doing wrong over here?
Please, no if statements if possible since I’m going to largely expand this array in the future.
You want curNameString to be equal to curNameTexture.
Well, after you set curNameString to be random, you can say curNameTexture = curNameString and vice versa.
Maybe I didnt understand what you want, but if this is what you are looking for, it is as simple as that
Almost there - I wanted the curNameTexture to be equal to curNameString.
“curNameTexture = curNameString” and vice versa isn’t working that way since you can not connect a Texture with a String.
Below is a simplistic explaining of what should go on, just to clarify:
I solved it by adding two lines. It appears that if you create a third integer and use this one as the Random.Range-thing, you can you use this for both (Texture2D and String) indexes:
//Users Setup:
public string [] myName = new string[5];
public Texture2D [] myTexture = new Texture2D[5];
private int curNameNumber;
private string curNameString;
private Texture2D curNameTexture;
// Use this for initialization
void Start () {
curNameNumber = Random.Range(0, myName.Length);
curNameString = myName[curNameNumber];
curNameTexture = myTexture[curNameNumber];
}
Now it will always show the right Texture to show what the person looks like.
For instance:
:)Thanks for helping me out. It’s not a false alert, but it bugged me for days.
Now arrays seem to get a bit more logical to me.