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

Resolved How to set references to instantiated instances?

Discussion in 'Scripting' started by clurpslurp, Jul 25, 2023.

  1. clurpslurp

    clurpslurp

    Joined:
    Jul 3, 2023
    Posts:
    23
    My goal is something like this. On character entering a trigger box,text comes up, and when the user presses E, something will happen. But the player is required to have a prefab in their "hands" to allow that to happen when they enter the trigger.

    Because the prefab is instantiated i set it to an instance
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (newChainCanvas.enabled)
    4.         {
    5.             if (Input.GetKeyDown(KeyCode.E))
    6.             {
    7.                 newChainInstance = Instantiate(newChainPrefab, hand.position, Quaternion.identity);
    8.                 newChainInstance.transform.parent = hand;
    9.                 return;
    10.             }
    11.         }
    12.     }
    where newChainInstance is a static Serialized field.

    Then, in a seperate script attached the to trigger box, I'm checking if the prefab's transform is equal to the "hand"'s transform. So, the player is holding the object.
    Code (CSharp):
    1.     private void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.CompareTag("Player"))
    4.         {
    5.             Debug.Log("Player enetered trigger");
    6.             if (hand.position == newChainInstance.transform.position)//minigame finishedin chain fix script
    7.             {
    8.                 Debug.Log("is holding");
    9.             {
    10.          {
    11.      {
    12.  
    13.  
    My player is detected enetering the trigger box. But an error occurs on line 6 "NullReferenceException: Object reference not set to an instance of an object".
    I'm guessing that when newChainInstance was set in the first script, it ran under multiple conditions in the update function, so the other script cannot detect newChainInstance because they are running at the same time?? But I'm confused about that because it's updating every frame. I tried using return, but I don't think it actually does anything because Update is called every frame anyways.
    This suspicion is probabaly incorrect, but is there a reason that the reference isn't detected?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,561
    Don't guess, debug!

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that

    ALSO: this is extremely unlikely to work:

    Use a distance check instead.

    Here's why:

    Floating (float) point imprecision:

    Never test floating point (float) quantities for equality / inequality. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

    "Think of [floating point] as JPEG of numbers." - orionsyndrome on the Unity3D Forums
     
    clurpslurp likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    The trigger is checking the player's hand the moment they enter the trigger and is finding that newChainInstance hasn't been set. The player then presses E and sets newChainInstance but it's too late, the moment has passed, the trigger is no longer checking.

    OnTriggerEnter only happens once the moment an object enters and doesn't happen again until the object leaves and renters. Or if another object enters.

    You need OnTriggerStay:


    Code (CSharp):
    1.     private void OnTriggerStay(Collider other)
    2.     {
    3.         if (other.CompareTag("Player"))
    4.         {
    5.             Debug.Log("Player enetered trigger");
    6.             if (newChainInstance)
    7.             {
    8.                 Debug.Log("is holding");
    9.             }
    10.         }
    11.     }
     
    clurpslurp likes this.