Problems with Assigning GameObject Variables on A Prefab

Hi, I’ve been struggling with this problem for a while now;

So I have this prefab, which has children called squares:
6108683--665018--Prefab.png
And each of those children have a script, which has a GameObject variable called onThisSquare. By default it is null, and I’ve checked each child to make sure it is so.
6108683--665021--Variable.png
Then I have a piece of code which instantiates the prefab at Vector3.zero at Start(), and nothing else. When I open my project for the first time and hit play, the prefab appears normally. But problems start appearing because of one game object, which has this piece of code:

if(Input.GetKeyDown(KeyCode.Space))
        {
            Transform startTile = currentTiles[0].tile.transform;  //currentTiles is a list that stores the prefab
            GameObject startSquare = startTile.GetChild(Random.Range(0, startTile.childCount)).gameObject;
            Square startInfo = startSquare.GetComponent<Square>();
            transform.position = startSquare.transform.position;
            currentSquare = startSquare;
            startInfo.onThisSquare = gameObject;
            Debug.Log("On this square: " + startInfo.onThisSquare);
            Debug.Log("Current square: " + currentSquare);
        }

(So it should move my game object called Cube on a random square and set that square’s onThisSquare variable equal that gameObject)
The object moves to the correct location when I press space, but the variable doesn’t work properly. When I Debug.Log the onThisSquare variable, it is equal to my cube game object as it should…
6108683--665024--Debug.png
…but on the inspector it shows null:
6108683--665027--CurrentSquare.png
So that’s my first problem. But it gets weird.
When I stop my game, and then start it again, the inspector then shows the variable. Which is weird since on Start() no variables are assigned and I haven’t pressed space yet.
6108683--665033--WrongVariable.png
Now, if I move my cube again with space, the same problem appears.
This time currentSquare is Square12.
In the console it’s onThisSquare equals Cube.
In the inspector it’s onThisSquare equals null.
I restart the game.
And now BOTH Square7 and Square12 have Cube as their onThisSquare variable. Right from the Start().
And the problem repeats when I hit space, restart and so on.

I have checked that there aren’t any other scripts messing with these variables, and for the sake of testing, I’ve pretty much disabled all other scripts and methods.

So if anyone can help out a beginner with the variable problem and explain why prefabs work the way they do, it would be much appreciated.

I’m a bit lost about what’s a prefab and what’s an instance of the prefab inn your explanation. It seems possible you are making changes to the prefab and expecting instances of the prefab to pick the change up or something? It’s hard to tell. For example I can’t tell if your “currentTiles” array is an array of prefabs or an array of instances in the scene.

One thing it seems like you might be doing is trying to get a prefab to have a reference to a GameObject in the scene. That’s generally a no-go.

Can you clarify a bit which script has which code and is on which object. Also how does the “currentTiles” array get initialized and which script is that a part of?

When I now looked at it, the answer was actually that I tried to change my prefab. I had an another list of game objects which stored my prefab, and when I instantiated the prefab, I added that prefab on my currentTiles list. The prefab, not the instance I just made. So it used to be something like this:

GameObject tile = prefabList[0];
Instantiate(tile, Vector3.zero, transform.rotation);
currentTiles.Add(tile);

When it should’ve been like this:

GameObject tile = prefabList[0];
GameObject instance = Instantiate(tile, Vector3.zero, tile.transfrom.rotation);
currentTiles.Add(instance);

I guess since I had the instance in my scene view, I kept assuming I was referencing to it, not the prefab itself…but thank you for pointing it out.

1 Like