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

Accessing object/var in other script

Discussion in 'Scripting' started by luckie12, Feb 26, 2015.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Greetings people!

    I'm kinda new to C# so im trying some random stuff out, but i have a problem that i cant figure out.

    I have 2 scripts:

    Money.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Money : MonoBehaviour {
    5.  
    6.     public int money = 0;
    7.     public GUIText moneyText;
    8.     public GUIText winText;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.         if(Input.GetKeyUp (KeyCode.Space))
    18.         {
    19.         money = money + 1;
    20.         SetMoney();
    21.         }
    22.     }
    23.     void SetMoney()
    24.     {
    25.         moneyText.text = "Money: " + money.ToString ();
    26.         if (money >= 1000)
    27.         {
    28.             winText.text = "YOU WON " + money.ToString() + " DOLLARS";
    29.         }
    30.     }
    31. }
    32.  

    and

    Pickup.cs


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Pickup : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.     void OnTriggerEnter(Collider col)
    11.     {
    12.         if (col.gameObject.name == "Cube")
    13.         {
    14.             Money other = gameObject.GetComponentInChildren<Money>();
    15.             other.moneyText.text = "Cube";
    16.             Destroy(col.gameObject);
    17.         }
    18.     }
    19.     // Update is called once per frames
    20.     void Update () {
    21.  
    22.     }
    23. }
    24.  
    The Pickup.cs script is attached to a Cube named Cube, and the Money.cs is attached to an empty gameobject that has an GUIText in it. But the problem is nothing happens only this error appears whenever i pickup the Cube:

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Pickup.OnTriggerEnter (UnityEngine.Collider col) (at Assets/Pickup.cs:15)
    3.  
    I want it whenever the OnTriggerEnter function gets called when picking up the Cube, i want it that it calls the Money.cs script and change the money count one up.

    What am i doing wrong?

    With kind regards,

    Luc
     
  2. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    try

    Code (CSharp):
    1.   void OnTriggerEnter(Collider col)
    2.     {
    3.         if (col.gameObject.name == "Cube")
    4.         {
    5.             Money other = col.gameObject.GetComponentInChildren<Money>();
    6.             other.moneyText.text = "Cube";
    7.             Destroy(col.gameObject);
    8.         }
    9.     }
     
  3. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Still the same error :/
     
  4. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    Have you actually set your text's within the inspector?
     
  5. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    I did, it says Money: 0 :)
     
  6. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    your moneytext and wintext both have a guitext within them and dont say None(GuiText)?
     
  7. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
  8. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    Is the Red Cube being picked up by another object like the player and the player has the trigger on them?
    if so place this on the object that's picking up the object

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PickUp : MonoBehaviour {
    5.    
    6.     void OnTriggerEnter(Collider col)
    7.     {
    8.         if (col.gameObject.name == "Cube")
    9.         {
    10.             GameObject MoneyText = GameObject.Find ("MoneyText");
    11.             Money other = MoneyText.gameObject.GetComponent<Money>();
    12.             other.moneyText.text = "Cube";
    13.             Destroy(col.gameObject);
    14.         }
    15.     }
    16. }
    17.  
    If you are making it so the player is picking up money why not make the money (cube) the trigger and use the tag player then if you need to pass the name you can use the this.gameobject.name i always think the object top be picked up should have the trigger on them i may have misinterpreted what your trying to do though ahha.
     
  9. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    So, just place the PickUp.cs ONLY on the cube?

    Basically what the cube does when i touch it ( what i try to do);

    If my player picksup the Cube, then my money value gets changed by +=1

    The money variable stores the amount of money, the moneyText shows me the money in the game with a GUIText.

    PS I have IsTrigger enabled on the cube
     
    Last edited: Feb 27, 2015
  10. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    If the cube has the trigger on it and the player is tryign to pick it up change the script on the cube too:

    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.    
    4.     public class PickUp : MonoBehaviour {
    5.      
    6.         void OnTriggerEnter(Collider col)
    7.         {
    8.             if (col.gameObject.name == "First Person Controller")
    9.             {
    10.                 GameObject MoneyText = GameObject.Find ("MoneyText");
    11.                 Money other = MoneyText.gameObject.GetComponent<Money>();
    12.                 other.money ++;
    13.                 Destroy(this.gameObject);
    14.             }
    15.         }
    16.     }
    17.    
    18.  
    That should increment the money int,
    Have you considered using the new unity gui?
     
  11. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    the new unity gui? you mean with the Canvas? i dont get the canvas at all :$
     
  12. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    Still the same error btw
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Make sure you look for the exact name with @Jmanpie's solution. It should be "moneyText" and not "MoneyText" in GameObject.Find(..).
     
  14. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    That works ^, but whenever i pick it up, it doesnt show it in the GUIText,
    i made a lil part on the code whenever i press Space my money counts 1 up.
    If i pickup the box now, the count goes from 0 to 2 whenever is jump, so it adds it up but doesnt show in my screen until i jump :O
     
  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Do you update the GUIText's text in any way?
    The latest solution by Jmanpie does only increment money, but does not alter the text of the GUIText.
    You could simply make a method in your Money script which 1) increments money 2) updates the text of the GUIText component.
    Instead of 'other.money++' call the method instead.
     
  16. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    it does, at the SetMoney(); part but that only calls every Update();
     
  17. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    As i mentioned, you can either move 'money++' into the method and replace
    Code (CSharp):
    1. other.money ++;
    with
    Code (CSharp):
    1. other.SetMoney();
    Or leave money++ as it is in the trigger method and just add the second line.
     
  18. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    the SetMoney is red on the script, but gives no error, but also does not work haha

    Code (CSharp):
    1.         if (col.gameObject.name == "First Person Controller")
    2.         {
    3.             GameObject moneyText = GameObject.Find ("moneyText");
    4.             Money other = moneyText.gameObject.GetComponent<Money>();
    5.             other.money ++;
    6.             other.SetMoney();
    7.             Destroy(this.gameObject);
    8.         }
     
    Last edited: Feb 27, 2015
  19. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Maybe because it's not declared as public?
     
  20. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    placed Public infront of void, its not red atm, but doesnt update :/

    Code (CSharp):
    1.     public void SetMoney()
    2.     {
    3.         moneyText.text = "Money: " + money.ToString ();
    4.         if (money >= 1000)
    5.         {
    6.             winText.text = "YOU WON " + money.ToString() + " DOLLARS";
    7.         }
    8.     }
     
  21. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    165
    changed the
    Code (CSharp):
    1. public int money = 0;
    to
    Code (CSharp):
    1. public float money = 0;
    works now!

    Edit: the public int works too, my Unity and Monodevelop were not sync'd.
    Thanks for the help both of you!
     
    Last edited: Feb 27, 2015