Search Unity

Why isn't this gameObject persisting?

Discussion in 'Getting Started' started by MarkSteere, Mar 8, 2023.

  1. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    The gameObject tile_residence is fine in the Start method. But then later in OnMouseDown, it has become null.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Checker : MonoBehaviour
    6. {
    7.     //
    8.     //  Place checker on this tile during initial board setup.  Setup_Tile set in Unity editor.
    9.     //
    10.     public GameObject Setup_Tile;
    11.  
    12.     //
    13.     //  Tile this checker currently resides at
    14.     //
    15.     GameObject tile_residence;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         transform.position = Setup_Tile.transform.position;
    21.  
    22.         tile_residence = Setup_Tile;                                        // Set this checker's residence tile
    23.         Debug.Log(tile_residence.transform.name);
    24.  
    25.         tile_residence.GetComponent<Tile>().resident_checker = gameObject;  // Set this checker as the resident of the residence tile
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.        
    32.     }
    33.  
    34.     void OnMouseDown()
    35.     {
    36.         //Debug.Log(transform.parent.name + ", " + transform.name);
    37.  
    38.         if (tile_residence == null)
    39.         {
    40.             Debug.Log("tile_residence == null");
    41.  
    42.         }
    43.  
    44.         //Debug.Log(tile_residence.transform.name);
    45.         /*
    46.         if (this.Setup_Tile != null)
    47.         {
    48.             //Debug.Log(Setup_Tile.name);
    49.  
    50.             //Debug.Log(tile_residence.transform.name);
    51.  
    52.             //Debug.Log(tile_residence.GetComponent<Tile>().isSourceTile);
    53.         }
    54.         */
    55.     }
    56. }
    57.  
     
  2. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Maybe I can work around this and just use the original Setup_Tiles without making copies. But I'd still be interested in knowing the problem here, just for my own education, if anyone knows.
     
  3. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    No, apparently cannot change objects set in Unity editor...
     
  4. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    I think I have a clue. It seems to be a parent-child thing. The tutorial guy said it would save me a TON of trouble if I had tile objects contained in tile container objects. So far it's been just the opposite.
     
  5. MarkSteere

    MarkSteere

    Joined:
    Dec 31, 2022
    Posts:
    54
    Got it. Required the line...

    Code (CSharp):
    1.         Debug.Log(transform.parent.GetComponent<Checker>().tile_residence);
    2.  
    3.  
    That was a tough nut. Learned something about Unity in the process.