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

Unable to get player Money from Game Script

Discussion in 'Scripting' started by jackson_31, Feb 19, 2015.

  1. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    I have 3 game objects, Player, Main and Text.
    Player and Main are independent game objects in the Hierarchy whereas Text is a sub game object of the Canvas Gameobject.

    Player has Player.css script attached,
    Main has Game.css script attached
    Text has TextScript.css attached

    scripts are as follows

    Player Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player
    5. {
    6.     public int Health{ get; set;}
    7.     public int Money{ get; set;}
    8. }
    Game Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Game1 : MonoBehaviour
    5. {
    6.     private int money;
    7.  
    8.     void Start ()
    9.     {
    10.         Player player1 = new Player();
    11.         player1.Money=50000;
    12.     }
    13.  
    14. }
    15.    
    I am not sure what needs to go into the textScript in order to see the player Money
     
  2. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    you need to store a variable within the Player script a private one int money.
    you need to call it this way

    Code (CSharp):
    1. Player1.Money = 1000;

    Code (CSharp):
    1. int money = 0;
    2. public int Money
    3. {
    4.     get
    5.     {
    6.         return money;
    7.     }
    8.     set
    9.     {
    10.         money = Value;
    11.     }
    12. }
    in unity it works in compoentns so your script might not be accessable using Player player1 = new player();


    Code (CSharp):
    1. GameObject Player = GameObject.Find ("MainPlayer");
    2.         YourScript yourScript = Player.GetComponent<YourScript>();
    3.  
    4.         int Number = yourScript.Money;
     
  3. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    Not really. C# can do auto-implemented properties. https://msdn.microsoft.com/en-us/library/bb384054.aspx
    This is also not nessecary, because the player script does not derive from MonoBehaviour, it is not really a "Unity component", so Player player = new Player(); should work fine.

    Now, on to the problem at hand.

    You say you have a Player object with the Player script attached, that seems impossible judging by your code, because (as mentioned) your Player script does not derive from MonoBehaviour. You should derive it and use the method Jmanpie provided to access it.

    Or you should keep code as is, but then you cannot attach it to a gameobject, but only keep reference of the class in your code (this way it cant be directly connected to a gameobject, but you can still use the values in it)
     
  4. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    Your "Player.cs" must inherit from MonoBehaviour (as Game.cs does). You can't add a script to a GameObject if the latter doesn't inherit from MonoBehaviour.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     public int Health{ get; set;}
    7.     public int Money{ get; set;}
    8. }

    Also, you can't use "new" on a Component (= something which inherits from MonoBehaviour). "new" is kind of call when your GameObject is instantiated. So you need to have a reference to your Player inside your Game :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Game1 : MonoBehaviour
    5. {
    6.     public Player myPlayer;
    7.  
    8.     void Start ()
    9.     {
    10.         player.Money=50000;
    11.     }
    12.  
    13. }
    14.    
    From the inspector, you drag'n'drop your Player to the variable field "myPlayer".

    Or, you can do as @Jmanpie shown you.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Game1 : MonoBehaviour
    5. {
    6.     public Player myPlayer;
    7.  
    8.     void Awake() {
    9.         GameObject goPlayer = GameObject.Find("PlayerGameObjectName");
    10.  
    11.         if (myPlayer != null) {
    12.            myPlayer = goPlayer.GetComponent<Player>();
    13.         }
    14.     }
    15.     void Start ()
    16.     {
    17.         myPlayer.Money=50000;
    18.     }
    19.  
    20. }
    21.    
     
  5. jackson_31

    jackson_31

    Joined:
    Sep 20, 2014
    Posts:
    84
    Thanks for these replys. So say I want to keep player script as is, ie not derived from monobehavior. my game script now works. However how can I access the player money from another script? I am creating a new player in the game script, so from the text script I cant seem to access the player created in game script.
     
  6. Sbizz

    Sbizz

    Joined:
    Oct 2, 2014
    Posts:
    250
    You need a reference to your instance or you can create a static variable (Google: Singleton)