Unity 3d C# array issues

Hi everyone, I have been having several problems with an array in my script.When I type public string[ ] ExampleArray = new string[4]; it doesn’t set ExampleArray to equal 4 strings.I have to go into the inspector and manually set it. Then I found out if you declare ExampleArray then declare ExampleArray = new string[4]; in void start then it will set it to 4 strings.But if I type in a command like ExampleArray[0] = Text[a]; it doesn’t set the first string in the array to equal text[a]. Why is it not responding and Why am I having these issues?Does unity not work well with arrays?

public string[]text;
//public string[] ExampleArray = new string[4];Doesn't work must be set to equal to new string[4] in void start
public string[] ExampleArray 
void Start(){
ExampleArray = new string[4]
}
void OnGUI(){
text = new string[]{"text1","text2","text3","text4"};
for (int a = 0; a < Text.Length; a++)
if (GUILayout.Button("Button", GUILayout.Width(75), GUILayout.Height(25))){
ExampleArray[0] = Text[a];//Does not set ExampleArray[0] to text[a]
   }
}

Unity works perfectly fine with arrays. If you make a variable public, then you set up the array in the inspector. If you want to control it all through code, then you should probably not make it public. You definitely should not make a new array in OnGUI; that can’t possibly work since you are creating a new array every frame. Instead you should declare the array outside OnGUI. (Also, keep your variables consistent…you have “text” and “Text”, which are different things. Follow the Unity convention of using lowercase for variables and uppercase for classes and methods.)

–Eric

What’s wrong with putting text = new string[ ]{“text1”,“text2”,“text3”,“text4”}; in OnGUI?I haven’t encountered any problems with that.

@.@ its like telling the computer that every time you draw your GUI, i want you to create 4 strings and store them in this container i call an array. These are new strings every time, so never save the old ones even though they are the same values and mean the same things to me.

I don’t know where this Text variable comes from, but its not the text variable your storing those same 4 string values in. Case is very important to scripting and coding in generaly Text is not the same thing as text or tExt or tEXT or TEXT.
And of course, hello again legendary Eric5h5.

As I said, you’re creating a new array every frame. This is bad because it continuously allocates memory (which does get cleaned up, but it’s poor practice and leads to degraded performance). Also it would prevent you from changing the values elsewhere if needed, since the array is constantly being reset.

No, but thank you. :wink:

–Eric

So where would I want to declare new strings.Void start or a custom function maybe?

Hope this helps a bit.

using UnityEngine;
using System;

public class TestingStuff : MonoBehaviour
{
	public string [] text = {"text1", "text2", "text3", "text4"};

	public string [] exampleArray;

	public void Start()
	{
		exampleArray = new string[4];
		exampleArray[0] = "This is my other strings";
		exampleArray[1] = exampleArray[0].Replace(" ", "");
		for(int i = exampleArray[0].Length -1; i >= 0; --i)
		{
			exampleArray[2] += exampleArray[0][i];
		}
	}

	public void OnGUI()
	{
		GUILayout.BeginVertical();
		foreach(var word in text)
			GUILayout.Label(word);
		foreach(var moreWords in exampleArray)
			GUILayout.Label(moreWords);
		GUILayout.EndVertical();
	}
}