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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Missing Reference Exception

Discussion in 'Editor & General Support' started by wmmayfield, May 29, 2018.

  1. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    I'm new to Unity and to coding. I've tried a number of solutions to correct the Missing Reference Exception. I have not been able to find a solution. I've attached the script below. I'm using boulders as hazards that the player must avoid in this game I'm building. When the player's raft hits the boulder in the river the raft flips. I welcome any help anyone can offer to correct this error.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;


    public class simpleCollision : MonoBehaviour {
    public GameObject raft;
    public GameObject raft1Prefab;
    public Transform raftbracket;
    public Text messageText;
    public Text gameOverText;


    private bool gameOver;
    private bool restart;



    void Start ()
    {
    restart = false;
    messageText.text = "";
    gameOver = false;

    }
    void Update ()
    {

    if(raft == null)
    {
    raft = GameObject.FindWithTag ("Raft");
    }
    if (restart) {
    if (Input.GetKeyDown (KeyCode.R)) {
    SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
    }
    }
    }
    // Use this for initialization
    void OnTriggerEnter(Collider other)
    {
    if(other.CompareTag ("Raft"))
    Instantiate (raft1Prefab, raftbracket.transform.position, raftbracket.transform.rotation);
    Destroy (other.gameObject);

    gameOverText.text = "Game Over!";
    gameOver = true;
    messageText.text = "Press 'R' to restart";
    restart = true;

    }

    }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,822
  3. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    I've supplied my script with tags and a screen shot of the error message from the console. Any coaching you can provide will be appreciated. I'm very new and teaching myself. thanks

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UI;


    public class simpleCollision : MonoBehaviour {
    public GameObject raft;
    public GameObject raft1Prefab;
    public Transform raftbracket;
    public Text messageText;
    public Text gameOverText;
    private bool gameOver;
    private bool restart;

    // re-establilshing opening conditions
    void Start ()
    {
    restart = false;
    messageText.text = "";
    gameOver = false;
    }
    void Update ()
    {
    //make raft null to acknowledge the raft has been destoyed
    if(raft == null)
    {
    raft = GameObject.FindWithTag ("Raft");
    }
    // restarting the game
    if (restart) {
    if (Input.GetKeyDown (KeyCode.R)) {
    SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex);
    }
    }
    }
    // setting up collision results: switching raft for raft1prefab, destroying raft telling player game over and restart
    void OnTriggerEnter(Collider other)
    {

    Instantiate (raft1Prefab, raftbracket.transform.position, raftbracket.transform.rotation);
    Destroy (other.gameObject);
    gameOverText.text = "Game Over!";
    gameOver = true;
    messageText.text = "Press 'R' to restart";
    restart = true;
    }

    }
    upload_2018-6-1_12-46-27.png
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  5. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Thanks for noticing that ! I've attached the FollowTarget script. I just never thought my camera script would be the issue.

    Any coaching will be appreciated.

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

    public class FollowTarget : MonoBehaviour {

    public GameObject player;
    public Vector3 offset;

    void Start ()
    {

    offset = transform.position - player.transform.position;
    }


    void LateUpdate ()
    {
    transform.position = player.transform.position + offset;

    }
    }
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Please post code using "code" tags as in the link I included. You're making it unnecessarily difficult to find line 19 of your script by not doing so. And if the line numbers shift, please say which line is the original line 19.
     
  7. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Sorry for the missing tags.

    I've attached a new copy of the script with line numbers.

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

    public class FollowTarget : MonoBehaviour {

    public GameObject player;
    public Vector3 offset;
    // wanting the main camera to follow the player raft
    // establishing camera position
    11. void Start ()
    {

    offset = transform.position - player.transform.position;
    }

    //updating camera positioin
    void LateUpdate ()
    19. {
    transform.position = player.transform.position + offset;

    }
    }
     
  8. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    344
    Code (CSharp):
    1. public string ThisIsACodeTag;
    The problem though, is your 'player' GameObject is null. I'm assuming you meant to set it in the inspector, but it is an empty (missing) reference.

    An ugly fix

    If you take the player GameObject in the hierarchy, and add a tag of Player, you could do this instead

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FollowTarget : MonoBehaviour {
    6.  
    7.   public GameObject player;
    8.   public Vector3 offset;
    9.  
    10.   void Start ()
    11.   {
    12.     if (player==null) {
    13.       player = GameObject.FindWithTag("Player");
    14.     }
    15.     offset = transform.position - player.transform.position;
    16.   }
    17.  
    18.   void LateUpdate ()
    19.   {
    20.     transform.position = player.transform.position + offset;
    21.   }
    22. }
    23.  
     
    Last edited: Jun 1, 2018
  9. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Lloyd,

    I appreciate your time and help. I'll give it a shot and let you know.

    Thanks

    William
     
  10. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Lloyd,

    I've made the changes you suggested bu there is no change in the error message except it is now line 22.( which is line 3 below). Is there another way I could have the main camera follow the player?
    1. void LateUpdate ()
    2. {
    3. transform.position = player.transform.position + offset;
    4. }
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Did you assign your player object the "Player" tag? If so, does the player object exist at the time Start in FollowTarget is run?

    Also, can you please just click the link I put in comment #4 and follow the instructions for using "code" tags when posting code?
     
  12. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Joe,

    Thanks fo the link. I got it.

    object is assigned with tag "Player". No it does not exist at the time Start in FollowTarget is run.

    Question, the error does not appear in the console until after Player collides and is destroyed. Could Player be null during the LateUpdate in FollowTarget and causing the error notice? If so would I need to reestablish Player at the beginning of LateUpdate?

    Thanks
     
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If player is going to be null for just a few frames, I would just do this and not worry about it, unless you really need to move transform.position every single frame.

    Code (csharp):
    1. void LateUpdate ()
    2. {
    3.     if (player != null)
    4.     {
    5.         transform.position = player.transform.position + offset;
    6.     }
    7. }
     
  14. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Joe,

    Your suggestion makes a lot of sense. I'll give it a try and let you know. In the mean time, thanks for your time and suggestions.

    All the best,

    William
     
  15. wmmayfield

    wmmayfield

    Joined:
    Nov 2, 2017
    Posts:
    18
    Joe,

    You're great! It worked like a charm!