Search Unity

basic dialogue system via OnTriggerEnter in unity (cant set obj ref to instance of obj)

Discussion in 'Scripting' started by jay_m1, Nov 22, 2020.

  1. jay_m1

    jay_m1

    Joined:
    Nov 18, 2019
    Posts:
    3
    Hello

    I am attempting to make a simple dialogue system where a player enters a box collider which enables a Canvas object on enter and disables on exit. super simple and maybe I am missing something but most tutorials I found show you how to affect the collider & the other, but not a separate object.
    i use public Canvas GetCanvas and attach the canvas in the Inspector. I cant seem to get private to work

    I can't see how to reference the canvas in order to enable it. with this script i have no compiler errors in VS but I get a NullReferenceException object ref not set to instance of object. anyone know what im doing wrong?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Triggerscript : MonoBehaviour

    {


    private Canvas Dialogue;

    void OnTriggerEnter(Collider collider)
    {

    //add ui
    Dialogue.GetComponent<Canvas>().enabled = true;



    }


    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You haven't assigned any value to "Dialogue". There's a few ways to do so. The simplest is to make Dialogue serializable, and assign the value in the inspector. E.g. make your code like this:
    Code (CSharp):
    1. [SerializeField]
    2. private Canvas Dialogue;
    and then in the Unity inspector, drag and drop your dialogue object into the field that now shows up in the inspector for your Triggerscript.

    Also, if you're doing it this way, there's no reason to call GetComponent, since you already have the reference to the canvas. So your code inside OnTriggerEnter can just look like this:
    Code (CSharp):
    1. Dialogue.enabled = true;
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    That's just a warning and it can be ignored. Go ahead and assign the value by dragging and dropping in the inspector. As for the warning there's a discussion about it here: https://forum.unity.com/threads/suggesting-solutions-for-serializefield-warning.962112/

    One way to get rid of the warning is to remove [SerializeField] from your field and change "private" to "public".
     
  4. jay_m1

    jay_m1

    Joined:
    Nov 18, 2019
    Posts:
    3
    thank you. My mistake. If anyone else has got confused like me: I had disabled the object entirely, and assumed this code would enable that. Instead, make sure the object is enabled in the Inspector, but the Canvas Component is disabled.

    this script enables the Canvas component when the player enters a collider, and disables when the player exits.

    this is a very basic way to display Canvas / UI elements when a player enters a collider. there is an Image file attached to the Canvas that displays text.
    the collider must have IsTrigger enabled in the Inspector. the other collider must be tagged with "Player" and have a collider and rigidbody attached.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Triggerscript : MonoBehaviour

    {



    public Canvas Dialogue;

    void OnTriggerEnter(Collider collider)
    {

    //add ui
    Dialogue.enabled = true;



    }

    void OnTriggerExit(Collider collider)
    {

    Dialogue.enabled = false;

    }
    }
     
    PraetorBlue likes this.