Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem With 2 connected scripts

Discussion in 'Scripting' started by Cristaccio, Nov 21, 2017.

  1. Cristaccio

    Cristaccio

    Joined:
    Nov 20, 2017
    Posts:
    3
    Good morning, recently i made two connected scripts, i have some problem with them.
    Here's the first one:
    public GameObject Preview;
    public GameObject Text;
    public int EntranceCheck = 0;

    void OnTriggerEnter()
    {
    if (EntranceCheck == 0)
    {
    Preview.SetActive(true);
    }
    }

    void OnTriggerStay()
    {
    if (EntranceCheck == 0)
    {
    if (Input.GetKeyDown(KeyCode.F))
    {
    Preview.SetActive(false);
    Text.SetActive(true);
    EntranceCheck = 1;
    }
    }
    }

    void OnTriggerExit()
    {
    Preview.SetActive(false);
    Text.SetActive(false);
    }
    }

    When i start the game with just this script everything goes right, but the second script (that is connected with the first) doesnt work. Here is it

    ScriptProvaAvvenimenti Script1;
    public GameObject Preview2;
    public GameObject Text2;

    void Start()
    {
    Script1 = gameObject.GetComponent<ScriptProvaAvvenimenti>();
    }

    void OnTriggerEnter()
    {
    if(Script1.EntranceCheck == 1)
    {
    Anteprima2.SetActive(true);
    }
    }

    void OnTriggerStay()
    {
    if(Script1.EntranceCheck == 1)
    {
    if(Input.GetKeyDown(KeyCode.F))
    {
    Preview2.SetActive(false);
    Text2.SetActive(true);
    Script1.EntranceCheck = 2;
    }
    }
    }

    void OnTriggerExit()
    {
    Text2.SetActive(false);
    Preview2.SetActive(false);
    }
    }

    When i start the game and i try to enter the trigger where this script is attached the debugger says this:
    - NullReferenceException: Object reference not set to an instance of an object
    ScriptProvaAvvenimenti2.OnTriggerEnter () (at Assets/Script/Livello 2/ScriptProvaAvvenimenti2.cs:18)
    - NullReferenceException: Object reference not set to an instance of an object
    ScriptProvaAvvenimenti2.OnTriggerStay () (at Assets/Script/Livello 2/ScriptProvaAvvenimenti2.cs:26)

    What can i do with this? :/
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    1) Use code tags on the forum so people can easily read your code
    2) Which of the two scripts is ScriptProvaAvvenimenti2.cs?
    3) What lines in that script are 18 and 26?
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    As mentioned, please look at this thread for how to add code properly on the forums :) https://forum.unity.com/threads/using-code-tags-properly.143875/
    You can edit your post and keep it in mind for the future, too.

    Okay, my first question would be: Are the two scripts on the same game object? If not, then your GetComponent reference in Start, of the second script, is returning null. So, if it's on a different game object, I would suggest dragging and dropping it in the inspector. In fact, even if it's on the same game object, you can do that, too. :)