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

MissingReferenceException: Please help

Discussion in 'Scripting' started by JeevanjotSingh, Nov 19, 2014.

  1. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Sorry i was in the wrong forums first .
    i got some errors . I want to destroy my player , i use this


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5. public float speed;
    6. private int count;
    7. public GUIText countText;
    8. public GUIText winText;
    9. public int health = 100;
    10.  
    11. void start()
    12. {
    13.  
    14. count =0;
    15. SetCountText();
    16. winText.text ="";
    17. }
    18.  
    19. voidFixedUpdate()
    20. {
    21. float moveHorizontal =Input.GetAxis("Horizontal");
    22. float moveVertical =Input.GetAxis("Vertical");
    23. Vector3 movement =newVector3(moveHorizontal,0.0f,moveVertical);
    24. rigidbody.AddForce(movement * speed *Time.deltaTime);
    25. }
    26. void OnTriggerEnter(Collider other) {
    27.  
    28. if(other.gameObject.tag =="PickUp")
    29. {
    30. other.gameObject.SetActive(false);
    31. count = count +1;
    32. SetCountText();
    33. audio.Play();
    34. }
    35. }
    36. void SetCountText() { countText.text="Count: " + count.ToString(); if (count >= 12) { winText.text = "YOU WIN!"; } }
    37.  
    38. voidOnCollisionEnter(Collision col){
    39. if(col.transform.tag =="Enemy"){
    40. Destroy(gameObject);
    41. }
    42. }
    43. }


    and enemy -


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI: MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed;
    5.  
    6. privateTransform myTransform;
    7. publicint damage =25;
    8. publicGameObject otherGameObject;
    9. privatePlayerController playerScript;
    10. voidAwake(){
    11. myTransform = transform;
    12. }
    13. // Use this for initialization
    14. voidStart(){
    15. GameObject go =GameObject.FindGameObjectWithTag("Player");
    16. target = go.transform;
    17. }
    18. // Update is called once per frame
    19. voidUpdate(){
    20. Debug.DrawLine(target.position, myTransform.position,Color.cyan);
    21. //look at target
    22. //move towards target
    23. Vector3 directionTowardsPlayer =(target.position - myTransform.position).normalized;
    24. rigidbody.AddForce(directionTowardsPlayer * moveSpeed *Time.deltaTime);
    25. }
    26.  
    27. }

    i got this error - MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. CameraController.LateUpdate () (at Assets/Scripts/CameraController.cs:17)





    If i add this one in my CameraController.cs in LateUpdate() Function

    Code (CSharp):
    1. if (gameObject != null)
    2. {
    3. // Do something
    4. Destroy(gameObject);
    5. }


    then Console said "There are no audio listeners in the scene. Please ensure there is always one audio listener in the scene"
    and my camera didn't show anything
    and if i add in enemy nothing happens it will continuously saying about my first error if the player destroy .
    Please help me
     
    Last edited: Nov 19, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
  4. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You are destroying the game object that contains your camera. Thats why you are getting the audio listener error. Thats also why Unity cant render anything because there are no cameras in scene.
     
  5. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Yes that's true but how can i able to solve it .
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Is your PlayerController script attached to the camera?
     
  7. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    no but here is my camera controller script .

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     private Vector3 offset;
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.         offset = transform.position;
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void LateUpdate () {
    16.    
    17.  
    18.        
    19.  
    20.    
    21.  
    22.         transform.position = player.transform.position + offset;
    23.  
    24.  
    25.  
    26.  
    27.    
    28.     }
    29.  
    30.  
    31.  
    32.  
    33.  
    34.  
    35. }
    36.  
    37.  
    38.  


    no but i i do something like that then they also show me that same two errors that i got first .


    MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.Transform.get_position () (at






    MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    CameraController.LateUpdate () (at Assets/Scripts/CameraController.cs:22)
     
  8. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You are destroying the player GameObject in PlayerController but then are trying to access it again in your CameraController via:

    Code (CSharp):
    1. player.transform.position + offset;
    2.  
    Change lateupdate to:
    Code (CSharp):
    1. // Update is called once per frame
    2.     void LateUpdate () {
    3.  
    4.      
    5.  
    6. if(player != null)
    7. {
    8.         transform.position = player.transform.position + offset;
    9. }
    10.  
    11.     }
     
    naxovr and JeevanjotSingh like this.
  9. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    wow Thanks now one error is removed but the other one is still going on but wait i will try same method .
     
  10. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    in enemy it says player name does not exist
     
  11. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    so i take a - public gameobject player ; in enemy script
    but now the enemy didn't following me .
     
  12. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Please show me your PlayerController, EnemyController scripts
     
    JeevanjotSingh likes this.
  13. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Player Controller


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float speed;
    7.     private int count;
    8.     public GUIText countText;
    9.     public GUIText winText;
    10.     public int health = 100;
    11.  
    12.  
    13.  
    14.  
    15.     void start()
    16. {
    17.  
    18.         count = 0;
    19.         SetCountText();
    20.         winText.text = "";
    21. }
    22.  
    23.     void FixedUpdate ()
    24.  
    25.     {
    26.         float moveHorizontal = Input.GetAxis("Horizontal");
    27.         float moveVertical = Input.GetAxis("Vertical");
    28.         Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);
    29.  
    30.         rigidbody.AddForce (movement * speed * Time.deltaTime);
    31.    
    32.  
    33.     }
    34. void OnTriggerEnter(Collider other)
    35.     {
    36.    
    37.         if(other.gameObject.tag ==    "PickUp")
    38.             {
    39.                 other.gameObject.SetActive(false);
    40.             count = count + 1;
    41.             SetCountText();
    42.             audio.Play();
    43.             }
    44.     }
    45. void SetCountText()
    46.     {
    47.         countText.text="Count: " + count.ToString();
    48.         if (count >= 12)
    49.         {
    50.             winText.text = "YOU WIN!";
    51.         }
    52.     }
    53.  
    54.  
    55.  
    56.  
    57.     }
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  






    EnemyAI(This is my enemy controller script)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI: MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public int moveSpeed;
    8.     public int rotationSpeed;
    9.     public int health = 100;
    10.  
    11.     private Transform myTransform;
    12.  
    13.  
    14.     public GameObject player;
    15.  
    16.  
    17.     private PlayerController playerScript;
    18.  
    19.     void Awake(){
    20.  
    21.         myTransform = transform;
    22.    
    23.     }
    24.     // Use this for initialization
    25.     void Start () {
    26.         GameObject go = GameObject.FindGameObjectWithTag("Player");
    27.         target = go.transform;
    28.     }
    29.     // Update is called once per frame
    30.     void Update () {
    31.    
    32.         if(player != null)
    33.         {
    34.  
    35.         Debug.DrawLine(target.position, myTransform.position, Color.cyan);
    36.         //look at target
    37.        
    38.         //move towards target
    39.  
    40.         Vector3 directionTowardsPlayer = (target.position - myTransform.position).normalized;
    41.         rigidbody.AddForce (directionTowardsPlayer * moveSpeed * Time.deltaTime);
    42.  
    43.  
    44.  
    45.         }
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55. }
    56.  
    57.     void OnCollisionStay(Collision col){
    58.         if(col.transform.tag == "Player"){
    59.            
    60.             health-=2;
    61.            
    62.             if(health<=0)
    63.             {
    64.                 Destroy(col.gameObject);
    65.                
    66.             }
    67.         }
    68.  
    69.        
    70.  
    71. }
    72. }
     
  14. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    I Also Uploaded My video on youtube please let me know if you also want to know my game design .
     
  15. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Change you enemy AI start and update methods to this:

    Code (CSharp):
    1. // Use this for initialization
    2.     void Start () {
    3.         player = GameObject.FindGameObjectWithTag("Player");
    4.     }
    5.     // Update is called once per frame
    6.     void Update () {
    7.      
    8.         if(player != null)
    9.         {
    10.          
    11.             Debug.DrawLine(player.transform.position, transform.position, Color.cyan);
    12.             //look at target
    13.          
    14.             //move towards target
    15.          
    16.             Vector3 directionTowardsPlayer = (player.transform.position - transform.position).normalized;
    17.             rigidbody.AddForce (directionTowardsPlayer * moveSpeed * Time.deltaTime);
    18.          
    19.          
    20.          
    21.         }
     
    JeevanjotSingh likes this.
  16. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Thnaku very much
    You are genius .
     
  17. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Thank-you very much
    You are genius .
     
  18. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    One more thing
    How can i able to show my health as GUI or UI Text

    Code (CSharp):
    1. healthtext.text = "Health";
    this is gaving me an error
     
  19. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    What is the error?
     
  20. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Even though it is a dumb answer:
    Learn to code.
     
    Ricks likes this.
  21. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    yeh i know what you are saying and that's true but this is not the first error It makes some complication for me so i just gave an idea about the error actually . For me right know it's good way to learn code in c# by making my 1st game .
    I hope you understand .
     
  22. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I certainly understand that you have to learn somehow.
    Anyone who responded here who has a little bit of programming knowledge clearly sees that you don't understand the basics yet. We can easily see that because you are not sharing all the relevant information with us. It kind of feels like you are saying that you want to make a drawing and we need to explain you what a pen is and what colors are.

    In my opinion, it would make sense to take one step back and start to learn the basics first, before you start to work with scripts that you don't understand and with the goal to make a game. It is certainly up to you whether you want to proceed.
     
  23. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Thanks for advice i am agree with your suggestion . But please take me like as i never write code , i am just newbie with unity and it's library files to make a game(and i am learning them)
    I know this is just a simple typecasting from integer to string because i am a vb and c programmer ................................................... but at the last i am fully agree with you and take my step back :)
     
  24. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I certainly don't know what kind of skills you have and it is certainly not my intention to disqualify you in any way.
     
    JeevanjotSingh likes this.
  25. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Please don't think i am talikng with that way . I just want to get my game done *my first game" very very soon in some days so i can able to freely learn more languages on http://www.codecademy.com/learn (Great Site !)
    Do you have your own game . can you please show me your game .
     
  26. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Finally i got it my self

    healthtext.text="Health: " + health.ToString();
     
  27. JeevanjotSingh

    JeevanjotSingh

    Joined:
    Apr 30, 2014
    Posts:
    251
    Thanks to you and @Korno