Search Unity

Question Help fixing 'variable not assigned' error

Discussion in 'Scripting' started by tomraegan, Nov 27, 2022.

  1. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    137
    Hi,

    This code works, but it pumps out non-gamebreaking errors during play. It's "The variable JumpingPlatform of JumpCollisionDetector has not been assigned."

    The line throwing the error is Line 23: if (JumpingPlatform.CompareTag("JumpingPlatform") && (EnteredTrigger == true))

    Can someone please help me modify the code to stop the error? As I said, it doesn't affect the game, but I'm not a good enough coder to fix it myself.

    Thanks kindly!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class JumpCollisionDetector : MonoBehaviour
    6. {
    7.     public bool EnteredTrigger;
    8.     public GameObject JumpingPlatform;
    9.  
    10.     public void OnTriggerEnter(Collider col)
    11.     {
    12.         if (col.gameObject.CompareTag("JumpingPlatform"))
    13.         {
    14.             EnteredTrigger = true;
    15.             JumpingPlatform = col.gameObject;
    16.             JumpingPlatform.GetComponent<Collider>().isTrigger = true;
    17.         }
    18.     }
    19.  
    20.     IEnumerator OnTriggerExit(Collider col)
    21.     {
    22.  
    23.         if (JumpingPlatform.CompareTag("JumpingPlatform") && (EnteredTrigger == true)) --- ERROR
    24.          {
    25.             yield return new WaitForSeconds(.3f);
    26.             EnteredTrigger = false;
    27.             JumpingPlatform.GetComponent<Collider>().isTrigger = false;
    28.         }
    29.     }
    30. }
    31.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  3. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    137
    Thanks.

    Honestly, I've come a long way this year...from not scripting, to scripting! Admittedly, it's been difficult and I'm not very good. I've tried working out why...several times. o_O I honestly just need some help.
     
    Yoreki likes this.
  4. rickitz5

    rickitz5

    Joined:
    Aug 8, 2021
    Posts:
    31
    Have assigned your public variable JumpingPlatform in the inspector?
     
  5. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    137
    It gets assigned when it detects a viable platform. So I don't assign the public variable manually...it gets done runtime.

    A quick explanation...the script attaches to an object parented to the player to detect platforms above the player.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Then either one of two things needs to be true:

    1) you must guarantee enter gets hit BEFORE exit and that the code runs as expected,

    OR

    2) you need to check if it is null before trying to use it.
     
  7. rickitz5

    rickitz5

    Joined:
    Aug 8, 2021
    Posts:
    31
    As you are calling this on the OnTriggerExit callback, I am guessing the variable is not assigned at that time. If it is working fine just do
    Code (CSharp):
    1. if(JumpingPlatform == null) return;
    on line 22
     
  8. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    137
    Return didn't do it, but Yield break did :)

    Many thanks to you both for helping me along.
     
  9. rickitz5

    rickitz5

    Joined:
    Aug 8, 2021
    Posts:
    31
    My bad, missed you were in a IEnumerator, must sleep :p