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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Cached reference deleting itself

Discussion in 'Scripting' started by Alexsutton, Apr 1, 2020.

  1. Alexsutton

    Alexsutton

    Joined:
    May 27, 2016
    Posts:
    4
    I'm going mad here and I'm certain I'm missing something obvious but I cannot work it out.

    I have a script that controls the behaviour of a ball in my game, these are instantiated constantly whilst the game is active.
    Code (CSharp):
    1. public class BallBehaviour : MonoBehaviour
    2. {
    3.     private Rigidbody2D rb;
    4.     [SerializeField]
    5.     private float nudgeVelocity;
    6.     public string specialType;
    7.     public SpriteRenderer currentRenderer;
    8.     private Color currentColor;
    9.     //public GameObject gameController;
    10.     //public GameObject bgEffect;
    11.     public GameObject debris;
    12.        
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         currentRenderer = GetComponent<SpriteRenderer>();
    17.  
    18.         //This log statement returns "Gameplay", which is correct for the SpriteRenderer
    19.         Debug.Log(currentRenderer.sortingLayerName);
    20.  
    21.         rb = GetComponent<Rigidbody2D>();
    22.         //TODO Refine effects to different script
    23.         //Disables 'halo' for negative balls
    24.         if(specialType == "minus")
    25.         {
    26.             //bgEffect.SetActive(false);
    27.         }
    28.         currentColor = currentRenderer.color;
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void FixedUpdate()
    33.     {
    34.         CheckStuck();
    35.         //Check height of ball to destroy once off screen
    36.         if(transform.position.y < -5.5f)
    37.         {
    38.             DestroyBall();
    39.         }
    40.     }
    41.    
    42.     void CheckStuck()
    43.     {
    44.         if (rb.velocity.y == 0 && rb.velocity.x == 0)
    45.         {
    46.             rb.velocity = new Vector3(nudgeVelocity, 0f, 0f);
    47.         }
    48.     }
    49.     void DestroyBall()
    50.     {
    51.         Destroy(gameObject);
    52.     }
    53.     void OnCatch()
    54.     {
    55.         DestroyBall();
    56.     }
    57.     void OnBreak()
    58.     {
    59.         GameObject spawnedDebris = Instantiate(debris, transform.position, transform.rotation);
    60.         DebrisBehaviour spawnedBehaviour = spawnedDebris.GetComponent<DebrisBehaviour>();
    61.         spawnedBehaviour.ChangeColor(currentColor);
    62.         DestroyBall();
    63.     }
    64.     public void ChangeColor (Color inputColor)
    65.     {
    66.         Debug.Log("ChangeColor passed in: " + inputColor);
    67.  
    68.         //The below Debug statement throws an UnassignedReferenceException error for the currentRenderer field.
    69.  
    70.         Debug.Log(currentRenderer.sortingLayerName);
    71.         currentRenderer.color = inputColor;
    72.         currentColor = inputColor;
    73.     }
    74. }
    As far as I can see, the script is correctly finding the SpriteRenderer component during Start() as my Debug statement correctly retrieves one of it's attributes, however by the time I have called ChangeColor from another script, it has 'forgotten' about that reference and throws an error.

    I've used Visual Studio's Find All References to check I've not done something to it in another script somehow but the only references to currentRenderer are in the above code.

    Can anyone see what I've done wrong or offer any clues as to why this is happening?

    Thanks in advance,
    Alex.
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Some thoughts.

    Are you calling ChangeColor to soon, before Start() has got the reference to the renderer?
    Is the BallBehaviour script even enabled, and are you calling ChangeColor() of the right, and enabled, instance of the ball?

    What does the error say in the Console?
     
  3. Alexsutton

    Alexsutton

    Joined:
    May 27, 2016
    Posts:
    4
    You are 100% correct, arfish! I don't know quite enough about the order of execution in Unity to fully understand the sequence of events but moving my GetComponent requests from Start and into a new Awake method has solved the problem.

    Many thanks!
     
    arfish likes this.