- Home /
Script sets same value to other script in all objects instead of just one.
I'm adding a script component to an object in a loop like so:
{
clone = (GameObject)Instantiate(hexagon, new Vector3(x,y,z), transform.rotation);
clone.transform.parent = gameObject.transform;
clone.name = "gridTile:" + j + "," + i;
clone.AddComponent<Properties>();
}
Then later on in the code I'm looking for the object that was created (among many) and then setting a value in the Properties (again with a loop so that all objects have values in them) like so (edited to put more code that showed what I did wrong, should've made the new string array in the loop itself, thanks @Bonfire-Boy for the comments):
string[] conTemp = new string[6];
for loop{
clone = GameObject.Find("gridTile:" + j + "," + i);
{
string[] conTemp = {some numbers are set during code};
}
clone.GetComponent<Properties>().connections=conTemp;
}
Now the problem is, the code goes through them all and just sets them all the same value instead of different values. If I just run it out of the loop, it sets the right values but the loop somehow breaks it. For example the first one should have something like 1,2,3,4,5,6 as values but the second object should have 2,3,4,5,6,7 and instead they both get 2,3,4,5,6,7.
This is the only thing in the properties script:
public class Properties : MonoBehaviour {
public string[] connections = new string[6];
}
The whole code is uploaded as requested. As it is a messy work in progress code which is really long, I didn't want to put it in the question itself and couldn't figure out how to make a spoiler tag if it's possible, so here it is as an attachment : link text
Need the loop codes or methods code to help, codes fragment you provided are too vague.
I think there's a problem with this code...
clone = GameObject.Find("gridTile:" + j + "," + i);
{
string[] conTemp = {some numbers are set during code};
}
clone.GetComponent<Properties>().connections=conTemp;
The conTemp variable declared within the inner code block is local to that block. You're setting the connections variable to a different conTemp variable, which you're not modifying.
That's actually the only effect of those curly brackets. Is there a reason you've put them there and have you tried just removing them?
That's a block of code for ease of access (so I can keep track of what I wrote), it's actually in the same namespace as the other codes.
So those brackets are actually not doing anything (other than notation), and it does actually work, the code does the work and it sets the conTemp into the connections but my problem is, it doesn't just set it on one object, it just sets it on every object in the scene that I have set the connections beforehand (if I set the connections on one properties script then go on to the next one to set it, the previous one also gets set the same values).
Answer by Bonfire-Boy · Oct 08, 2016 at 06:34 PM
Ok, I can see now that you've added the full code as an attachment. Sorry for not having spotted that when I posted my comment, above. Incidentally, this does demonstrate the importance of posting your actual code and not oversimplifying it for a question, because the issue in the full code is completely different (practically opposite to that in the simplified code).
The problem in the full code is that you are reusing the conTemp
array and not creating it in the place shown in the simplified code!
You're looping through your tiles, (re-)populating that array and then setting the tile's connections
variable to point to the one-and-only conTemp
array. Like I say, this does not create a new array, it just makes that tile's connections
variable point to conTemp
. So, naturally, they all end up pointing to the same thing.
Instead, you actually want to do what the code in the question does, and create a new array each time through the loop.
So I think all you need to do is move the declaration of conTemp
from outside the loop to inside the loop, so that the real code looks more like the simplified code (but without those pointless curly brackets).
Wow, that could be it. I'll look into it and let you know if you should put this as the answer :)
Alright, yep that did it. I'll change the simplified code to include the wrong version and note at the bottom what the change was. You should just copy and paste this as an answer so I can select it.
Your answer
Follow this Question
Related Questions
where have i went wrong in my script (unity 5) 0 Answers
Refresh panel with prefab contained value from json that created using array 0 Answers
Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers
Manipulating the location of the VR controllers (Vive/SteamVR) in script 0 Answers
Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers