Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problems with Assigning GameObject Variables on A Prefab

Discussion in 'Scripting' started by AnAlienDream, Jul 20, 2020.

  1. AnAlienDream

    AnAlienDream

    Joined:
    Oct 19, 2019
    Posts:
    12
    Hi, I've been struggling with this problem for a while now;

    So I have this prefab, which has children called squares:
    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.
    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:

    Code (csharp):
    1. if(Input.GetKeyDown(KeyCode.Space))
    2.         {
    3.             Transform startTile = currentTiles[0].tile.transform;  //currentTiles is a list that stores the prefab
    4.             GameObject startSquare = startTile.GetChild(Random.Range(0, startTile.childCount)).gameObject;
    5.             Square startInfo = startSquare.GetComponent<Square>();
    6.             transform.position = startSquare.transform.position;
    7.             currentSquare = startSquare;
    8.             startInfo.onThisSquare = gameObject;
    9.             Debug.Log("On this square: " + startInfo.onThisSquare);
    10.             Debug.Log("Current square: " + currentSquare);
    11.         }
    (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...
    Debug.png
    ...but on the inspector it shows null:
    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.
    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.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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?
     
  3. AnAlienDream

    AnAlienDream

    Joined:
    Oct 19, 2019
    Posts:
    12
    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:
    Code (CSharp):
    1. GameObject tile = prefabList[0];
    2. Instantiate(tile, Vector3.zero, transform.rotation);
    3. currentTiles.Add(tile);
    When it should've been like this:
    Code (CSharp):
    1. GameObject tile = prefabList[0];
    2. GameObject instance = Instantiate(tile, Vector3.zero, tile.transfrom.rotation);
    3. 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.
     
    PraetorBlue likes this.