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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException

Discussion in 'Scripting' started by narmeenn, Mar 11, 2021.

  1. narmeenn

    narmeenn

    Joined:
    Oct 9, 2019
    Posts:
    6
    hello how can I fix the NullReferenceException: Object reference not set to an instance of an object, the error is in this line of code

    touchCurrentRenderer.material.color = touchOriginalColor;

    And below is my code

    public GameObject SnapLocation;
    public GameObject HoldObject;
    public GameObject HighLightObject;
    public bool isSnapped;
    private bool objectSnapped;
    private bool grabbed;
    private Renderer touchCurrentRenderer;
    private Color touchOriginalColor;
    public Color touchHighlightColor;
    public Renderer rend;
    private void Start()
    {

    }
    void Update()
    {
    grabbed = this.GetComponent<OVRGrabbable>().isGrabbed;
    objectSnapped = SnapLocation.GetComponent<SnapToLocation>().Snapped;
    Renderer rend = HighLightObject.GetComponent<Renderer>();
    rend.enabled = false;
    if (grabbed == true)
    {
    // Get Renderer component
    touchCurrentRenderer = HighLightObject.GetComponent<Renderer>();
    touchOriginalColor = touchCurrentRenderer.material.color;
    // Highlight Object
    touchCurrentRenderer.material.color = touchHighlightColor;
    rend.enabled = true;
    }
    if (grabbed == false)
    {
    touchCurrentRenderer.material.color = touchOriginalColor;
    rend.enabled = false;
    }
    if (objectSnapped == true)
    {
    GetComponent<Rigidbody>().isKinematic = true;
    transform.SetParent(HoldObject.transform);
    isSnapped = true;
    }
    if (objectSnapped == false && grabbed == false)
    {
    GetComponent<Rigidbody>().isKinematic = false;
    }
    }
     
  2. khanism

    khanism

    Joined:
    Dec 11, 2020
    Posts:
    9
    Which line of code is giving error ?

    It appears to be a missing component exception
     
  3. narmeenn

    narmeenn

    Joined:
    Oct 9, 2019
    Posts:
    6
    this one
    touchCurrentRenderer.material.color = touchOriginalColor;
     
  4. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    What is "touchOriginalColor"?

    It looks like you haven't defined this variable yet.
     
  5. narmeenn

    narmeenn

    Joined:
    Oct 9, 2019
    Posts:
    6
    as I understand I did it in this line or not?

    touchOriginalColor = touchCurrentRenderer.material.color;
     
  6. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Code (CSharp):
    1.  
    2. touchOriginalColor = touchCurrentRenderer.material.color;
    3. touchCurrentRenderer.material.color = touchHighlightColor;
    4.  
    This isn't going to work - what do you have as the value of touchCurrentRenderer.material.color in Unity?

    This is the most basic way of defining a colour in Unity:

    Code (CSharp):
    1.         Color newColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);
     
  7. narmeenn

    narmeenn

    Joined:
    Oct 9, 2019
    Posts:
    6
    Okay so at first I should define touchOriginalColor like this,

    Color touchOriginalColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);

    and then do this code like this

    touchOriginalColor = touchCurrentRenderer.material.color;

    Am sorry I don't have that experience in coding
     
  8. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    No it's because touchCurrentRenderer.material.color doesn't have a value.
    It's like saying 'What is something?' and then answering 'it's that' - the person still doesn't know what it is.

    Code (CSharp):
    1.  
    2. Color touchOriginalColor = new Color(0.3f, 0.4f, 0.6f, 0.3f);
    3. touchCurrentRenderer.material.color = touchOriginalColor;

    You can define all these and GetComponent stuff outside of Update so it's not looping over the lot, but it's probably not important here.
     
    khanism likes this.
  9. narmeenn

    narmeenn

    Joined:
    Oct 9, 2019
    Posts:
    6
    Ya because it still has the same problem when I tried it; a Null reference exception
     
  10. narmeenn

    narmeenn

    Joined:
    Oct 9, 2019
    Posts:
    6
    Yes it has a color property