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

N00b C# question getting int from 1 script to another

Discussion in 'Scripting' started by Dwaring28720, Oct 15, 2014.

  1. Dwaring28720

    Dwaring28720

    Joined:
    Sep 20, 2014
    Posts:
    10
    not sure if im even going in the right direction here i was creating a simple script with a list of base stats eg health mana etc with value, then i wanted my ui script to be able to access these values and pass them in my gui for now so the base stat script ive got

    Code (CSharp):
    1. public class base_stats{
    2.  
    3.     public static int starthp;
    4.     public base_stats()
    5.     {
    6. starthp = 44;    // Starting health
    7.     }
    8. }
    9.  
    then my ui script ive got

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class ui : MonoBehaviour{
    4.     void Start () {}
    5.     void Update () {}
    6.  
    7.     void OnGUI(){
    8.     GUI.Box(new Rect(1,1, 50, 50), (base_stats.starthp));
    9. }
    10. }
    i know ive got either the layout/format wrong here but im trying to learn so please no nasty comments i thought at first this was going to work, i declared the starthp variable as public then i gave it an actual value, then from the other script i thought i could just reference it in the way above, however i get 2 errors

    cant convert int to string,
    and an invalid argument in the GUI.Box line of code, which usually means ive got the syntax wrong, any help on this will be appreciated. i am trying to learn so any simple explanations for this is much appreciated
     
  2. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Try this:
    base_stats.starthp.ToString()

    And you might need to call the base_stats() function
     
  3. Dwaring28720

    Dwaring28720

    Joined:
    Sep 20, 2014
    Posts:
    10
    mmm hiya thanks for the speedy response, yeah i added To.String() and it did clear the error however it returns the value 0 instead of 44 which is in the base_stats,

    is this because im passing that value as a string when it should be an int? again im really new to this so im not sure im on the right page sorry if that seems a little dim

    thanks again
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Try this

    Code (CSharp):
    1. public class base_stats{
    2.  
    3.     public static int starthp = 44;
    4.  
    5. }
    6.  
    Then in the GUI.Box use base_stats.starthp.ToString(); or System.Convert.ToString(base_stats.starthp);

    Just a heads up, static variables are not reset upon changing scenes etc, so if you want to reset them you would have to make your own function

    Code (CSharp):
    1.  
    2. public static void ResetStats(){
    3.  starthp = 44;
    4. }
    5.  
     
    Dwaring28720 likes this.
  5. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Code (CSharp):
    1. public class base_stats{
    2.  
    3.  
    4.     public static int starthp;
    5.  
    6.     public base_stats()
    7.  
    8.     {
    9.  
    10. starthp = 44;    // Starting health
    11.  
    12.     }
    13.  
    14. }
    15.  
    16.  
    What is static, anyway? Static means that there's no copies of this, there's only one of these. Your base stats class isn't static, though. It has to be copied into a new instance. Where you've said starthp = 44 is in the code that runs when a new instance of base stats is created. Since you aren't creating a new instance of base stats, that code never runs!

    You should instead create a static constructor.

    Instead of public base_stats(), type static base_stats() and that code will run when your program starts.
     
  6. Dwaring28720

    Dwaring28720

    Joined:
    Sep 20, 2014
    Posts:
    10
    thanks both you literally

    System.Convert.ToString(base_stats.starthp);

    did the trick perfectly
     
  7. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718