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

help, Object reference error

Discussion in 'Scripting' started by JhonnyRage, Mar 16, 2019.

  1. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    54
    good morning, so my problem today is that im having a object reference error, while im sure that the float im tryng to use is there, here the 2 scripts.
    This script will make the player jump higher when enters in a colider.

    Code (CSharp):
    1. public class Trust : MonoBehaviour
    2. {
    3.     private Player Tomoe;
    4.  
    5.     void OnTriggerStay2D(Collider2D col)
    6.     {
    7.         if (col.gameObject.tag == "Player")
    8.         {
    9.             Jump();
    10.  
    11.         }
    12.     }
    13.     void Jump()
    14.     {
    15.         Tomoe.jumpPower = 20;
    16.         Tomoe.OJ = true; // this one call a particle to activate
    17.     }
    18. }
    and the player script

    Code (CSharp):
    1. public float jumpPower = 7.5f;
    2. public ParticleSystem OverJump;
    3.  
    4. //these are the parts that im using
    5.  
    6.             if (OJ == true)
    7.             {
    8.                 OverJump.Play();
    9.             }else
    10.             {
    11.                 OverJump.Stop();
    12.             }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,117
    You'll need to look at the error message for the specific line number. Someone in the error you'll probably see two numbers in parantheses, somthing like (15,9). The first number tells you the line number of the script where the error occurred.

    Anyway, without knowing which line number, we can assume it's probably the case that you haven't assigned the "Tomoe" property in the inspector, or you haven't assigned the "OverJump" property in the inspector. Make sure those fields have values in the inspector.
     
  3. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    54
    yeah, sorry i forgot, the error is on the line 15, the one that manage the Jump power
    and both are private so is not like inspector matter
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,117
    If "Tomoe" is private, how would it ever have a value, then? You'll either need to assign a value in the inspector, or manually find it in your scene within your Start method.
     
    JhonnyRage likes this.
  5. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    54
    yeah... i figured it out at the end, ty