Search Unity

SetLifeText(); isn't working

Discussion in 'Scripting' started by AlexKunnen, Dec 4, 2017.

  1. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class destroybycontact : MonoBehaviour
    7. {
    8.     public GameObject explosion;
    9.     public GameObject playerexplosion;
    10.     public int scoreValue;
    11.     private gamecontroller gamecontroller;
    12.  
    13.     public Text lifeText;
    14.     public int life;
    15.  
    16.  
    17.     void Start ()
    18.     {
    19.         //healthText.text = new string ('∎', health);
    20.         GameObject gamecontrollerObject = GameObject.FindWithTag ("gamecontroller");
    21.  
    22.         if (gamecontrollerObject != null)
    23.         {
    24.             gamecontroller = gamecontrollerObject.GetComponent <gamecontroller> ();
    25.         }
    26.  
    27.         if (gamecontroller == null)
    28.         {
    29.         Debug.Log ("Cannot find 'gamecontroller' script");
    30.         }
    31.         life = 10;
    32.         SetLifeText ();
    33.     }
    34.  
    35.     void SetHealthText ()
    36.     {
    37.         lifeText.text = "Health:" + life.ToString ();
    38.         if (life <= 0)
    39.         {
    40.             gamecontroller.GameOver ();
    41.         }
    42.     }
    43.     void OnTriggerEnter (Collider other)
    44.     {
    45.             if (other.CompareTag ("boundary") || other.CompareTag ("Enemy")) {
    46.                 return;
    47.             }
    48.  
    49.             if (explosion != null) {
    50.                 Instantiate (explosion, transform.position, transform.rotation);
    51.             }
    52.  
    53.             if (other.tag == "player")
    54.             {
    55.                 Instantiate (playerexplosion, other.transform.position, other.transform.rotation);
    56.                 gamecontroller.GameOver ();
    57.                 life = life - 1;
    58.                 SetLifeText ();
    59.             }
    60.         //if (other.tag == "player")
    61.         //{
    62.         //    health = health - 1;
    63.         //    SetHealthText ();
    64.         //}
    65.         gamecontroller.AddScore (scoreValue);
    66.         Destroy (other.gameObject);
    67.         Destroy (gameObject);
    68.     }
    69. }
    Assets/scripts/destroybycontact.cs(32,3): error CS0103: The name `SetlifeText' does not exist in the current context
    Assets/scripts/destroybycontact.cs(58,5): error CS0103: The name `SetlifeText' does not exist in the current context
    All compiler errors have to be fixed before you can enter playmode!
    UnityEditor.SceneView:ShowCompileErrorNotification()
     
  2. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    I don't see any SetLifeText method. Don't you mean SetHealthText?
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You haven't ever declared a method called SetLifeText.

    You probably want to change line 35 to be SetLifeText.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    Please post the code that you're actually using and the errors you're actually getting. Otherwise it will be very hard to help you with anything.

    The error states that you're trying to call "SetlifeText". The code you posted has a method called "SetHealthText". If the method actually is called "SetLifeText", that also won't work, as "L" and "l" are not the same letter.
     
    Kiwasi likes this.
  5. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    Thanks all
     
  6. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    In Start.. Add this..

    Code (CSharp):
    1. lifeText = GetComponent<Text>();
     
    Last edited: Dec 5, 2017
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well you still haven't answered the question of where that SetlifeText() method is that your error you posted is referring to, since it isn't anywhere in the code you posted.
     
  8. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    I had high hopes when I saw this, but my script still isn't working :(
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What's the error now? Same or something different?
     
  10. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Basically youre calling
    Code (CSharp):
    1. SetLifeText ();
    .. But the code you posted doesnt seem to have any
    Code (CSharp):
    1. SetLifeText ()
    2. {
    3.  
    4. // Do the life stuff here
    5.  
    6. }
    You need to have this, along with what it is you want it to do..
     
  11. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    still the same
    NullReferenceException: Object reference not set to an instance of an object
    destroybycontact.SetLifeText () (at Assets/scripts/destroybycontact.cs:37)
    destroybycontact.Start () (at Assets/scripts/destroybycontact.cs:32)
     
  12. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    You have this..

    Code (CSharp):
    1. void SetHealthText ()
    2.     {
    3.         lifeText.text = "Health:" + life.ToString ();
    4.         if (life <= 0)
    5.         {
    6.             gamecontroller.GameOver ();
    7.         }
    8.     }
    now you need this..

    Code (CSharp):
    1. void SetLifeText ()
    2.     {
    3.         // That stuff
    4.     }
    or on line 32 you need to change
    Code (CSharp):
    1. SetLifeText();
    to
    Code (CSharp):
    1. SetHealthText();
     
  13. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Just change line 32 to
    Code (CSharp):
    1. SetHealthText();
    and then let us know if that worked..
     
  14. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    I changed that awhile ago, all life had been health at one point and I missed SetHealthText(); This is the code exactly as I have rn, still doesnt work.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class destroybycontact : MonoBehaviour
    7. {
    8.     public GameObject explosion;
    9.     public GameObject playerexplosion;
    10.     public int scoreValue;
    11.     private gamecontroller gamecontroller;
    12.  
    13.     public Text lifeText;
    14.     public int life;
    15.  
    16.     void Start ()
    17.     {
    18.         lifeText = GetComponent<Text>();
    19.         //healthText.text = new string ('∎', health);
    20.         GameObject gamecontrollerObject = GameObject.FindWithTag ("gamecontroller");
    21.  
    22.         if (gamecontrollerObject != null)
    23.         {
    24.             gamecontroller = gamecontrollerObject.GetComponent <gamecontroller> ();
    25.         }
    26.  
    27.         if (gamecontroller == null)
    28.         {
    29.         Debug.Log ("Cannot find 'gamecontroller' script");
    30.         }
    31.         life = 10;
    32.         SetLifeText ();
    33.     }
    34.  
    35.     void SetLifeText ()
    36.     {
    37.         lifeText.text = "Health:" + life.ToString ();
    38.         if (life <= 0)
    39.         {
    40.             gamecontroller.GameOver ();
    41.         }
    42.     }
    43.     void OnTriggerEnter (Collider other)
    44.     {
    45.             if (other.CompareTag ("boundary") || other.CompareTag ("Enemy")) {
    46.                 return;
    47.             }
    48.  
    49.             if (explosion != null) {
    50.                 Instantiate (explosion, transform.position, transform.rotation);
    51.             }
    52.  
    53.             if (other.tag == "player")
    54.             {
    55.                 Instantiate (playerexplosion, other.transform.position, other.transform.rotation);
    56.                 gamecontroller.GameOver ();
    57.                 life = life - 1;
    58.                 SetLifeText ();
    59.             }
    60.         //if (other.tag == "player")
    61.         //{
    62.         //    health = health - 1;
    63.         //    SetHealthText ();
    64.         //}
    65.         gamecontroller.AddScore (scoreValue);
    66.         Destroy (other.gameObject);
    67.         Destroy (gameObject);
    68.     }
    69. }
     
  15. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    have you tried
    Code (CSharp):
    1. public void SetLifeText ()
    2.     {
    3.         lifeText.text = "Health:" + life.ToString ();
    4.         if (life <= 0)
    5.         {
    6.             gamecontroller.GameOver ();
    7.         }
    8.     }
     
  16. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    With your code above, its still giving the error..

     
  17. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    yes it is
     
  18. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    still cant input the text into the field in the inspector either
     
  19. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    I have to tell you im stumped bud. Make sure the script is attached to something in the scene.. Then make sure the life text is also in the scene.. You should have no reason that you cant drop it into the text field of your script..
     
  20. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Is that the only error? and with the above script show what the exact erorrs its giving you now if you hit play..
     
  21. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    when you hit play its fine only when you make contact with an enemy ship and it goes through the if enemyship hits player health - 1 line.
     
  22. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    That is because you are destroying it..
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, are these being instantiated as you're playing? Is it that you can't drag the text to the prefab ?
     
  24. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    They are prefabs (asteroids and enemy ship) they instantiate in waves.
     
  25. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    but it looks like hes destroying the prefab with the script so there for the text isnt going to be available anymore
     
  26. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Maybe instead of
    Code (CSharp):
    1. Destroy (gameObject);
    you tell it specifically.. Like
    Code (CSharp):
    1. Destroy (explosion);
    or
    Code (CSharp):
    1. Destroy (playerexplosion);
     
  27. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    or make a different script with player text and make it a singleton so that you can access the player health and stuff. Like a PlayerManager script.. use that for your lifetext, healthtext, and such.. that way it wont be destroyed..
     
  28. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    I cant even set which GUI Text it is even before I hit play.
     
  29. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I swear I answered a question just like this recently.
    So, what you need to do is when you instantiate the enemy ships, you have to set the Text field from that point. Whichever script is creating them, should have the drag n drop from the inspector.
    This way, it'll work for ya...
     
  30. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Easiest way i find for instantiating is a GameManager script that has all my stuff. It doesnt get destroyed. You can call your prefabs and what not and then it will destroy them without any errors.
     
  31. AlexKunnen

    AlexKunnen

    Joined:
    Sep 22, 2017
    Posts:
    18
    Not at my computer at the moment but I will let you know if this works, thanks.