- Home /
Can't access string in another script
I'm trying to let the user choose which seed the game will load as their level, but I can't access the variable for some reason. What am I doing wrong??
public class SeedNumberScript : MonoBehaviour {
public string stringToEdit = "Enter A Seed Number";
void OnGUI() {
stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
if(GUI.Button(new Rect(10,400,1200,100), "Enter The World"))
{
Application.LoadLevel("NewIdeas&Tests");
}
}
}
Second Script
void Start () {
SeedNumberScript.stringToEdit == Random.seed;
for(int z = 0; z < GameHeight; z++)
{
for(int x = 0 ; x < GameWidth; x++)
{
float rnd = Random.value;
if(rnd <0.25f)
{
Instantiate(TileOne, new Vector3(x,0,z), Quaternion.identity);
}
else if(rnd <0.5f)
{
Instantiate(TileTwo, new Vector3(x,0,z), Quaternion.identity);
}
else
{
Instantiate(TileThree, new Vector3(x,0,z), Quaternion.identity);
}
}
}
}
}
I'm trying to access the String in the first scrip with the second script. I keep getting Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Answer by oasisunknown · Jun 07, 2014 at 03:34 AM
First I dont see a reference declared to the other script. try here for more information on that.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Components.html
but also make sure your doing the right call.
SeedNumberScript.stringToEdit == Random.seed;
the double == means check to see if this is THE SAME AS Random.Seed;
SeedNumberScript.stringToEdit = Random.seed;
This says SET .stringToEdit to Random.Seed;
Thanks for the responses guys since eric already had an unholy amount of karma ill give You best answer. He could drown in all his o.o
I appreciate that. but I am curious on which version of == Random.Seed did you need to use?
or was it just the GetComponent method that was needed?
Ill give you an update in a bit. Haven't had a chance to do it yet, had to go to bed then work.
I did need just one = because I no longer get an error on that line. I also need to get the component tho because its telling me an object reference is required. So then it looks like this
GetComponent<SeedNumberScript>().stringToEdit;
SeedNumberScript.stringToEdit = Random.seed;
but that gives me another Only assignment, call, increment, decrement, and new object expressions can be used as a statement error on the .stringToEdit; Did I set up the string wrong?
technically your not doing something wrong but look at it this way.
your saying take my string and make it an integer. your trying to fundamentally change the type of the string.
I don't know what your using the string for but if you want it to just display the number try.
SeedNumberScript.stringToEdit = "" +Random.seed;
this concatenates the int on the end of a string that has nothing in it. and that is acceptable in unity.
for more reference look up subjects on casting to different variable types.
https://www.youtube.com/watch?v=CVx6W2OdvLU∈dex=4&list=PL4BBB74C7D2A1049C
in this video around $$anonymous$$ute 18 he talks about specific cast operations in java but the concept still applies.
Answer by Eric5h5 · Jun 07, 2014 at 03:30 AM
You need to use GetComponent.
GetComponent<SeedNumberScript>().stringToEdit....
Your answer
![](https://koobas.hobune.stream/wayback/20220613150958im_/https://answers.unity.com/themes/thub/images/avi.jpg)