Search Unity

C# Calling Variables From Other Scripts

Discussion in 'Scripting' started by Nikola310, Dec 11, 2015.

  1. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hello everyone!
    I've been trying to find the solution to my problem for more than and hour now, and I can't find one.

    I have two scripts: Player Controller (which is also used for other things, and not just player movement) and API Script. I released my game to Gamejolt, and I'm making trophies for it. My game is advanced roll a ball.
    For unlocking the second trophy, you need to have 6 points on level 4.
    I want to acces the "count" int (which are the points- having 6 points advances you to the next level, but I haven't made level 5 so I can't use if (SceneManager.GetActiveScene().name=="Level5" to award the player the "You've completed level 4" trophy) which is in the PlayerController script, and see if it has a value of 6 in my API Script.
    Here is my API Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class APIScript : MonoBehaviour {
    6.  
    7.     GameObject Player = GameObject.Find ("Player");
    8.  
    9.     void Start ()
    10.     {
    11.         if (GameJolt.API.Manager.Instance.CurrentUser == null)
    12.             {
    13.             GameJolt.UI.Manager.Instance.ShowSignIn ();
    14.             }
    15.         if (SceneManager.GetActiveScene().name=="Level3")
    16.         {
    17.             GameJolt.API.Trophies.Unlock (46732);
    18.  
    19.         }
    20.         if (SceneManager.GetActiveScene().name=="Level4" /* I want count (instead of this comment ) to be the count int from my PlayerController class and see if it is 6*/ )
    21.         {
    22.             GameJolt.API.Trophies.Unlock (46733);
    23.         }
    24.     }
    25.        
    26. }
    27.  
    And here's my PlayerController script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.    
    8.     public float speed;
    9.     public Text CountText;
    10.     public static int count;
    11.     public Text WinText;
    12.     public AudioClip[] audioClip;
    13.     public PlayerController ThePlayer;
    14.     private Vector3 StartPosition;
    15.     private Quaternion StartRotation;
    16.     public DeathMenu TheDeathScreen;
    17.     public Text tip;
    18.  
    19.  
    20.     void Awake ()
    21.     {
    22.     }
    23.  
    24.     void Start ()
    25.     {
    26.         count = 0;
    27.         SetCountText ();
    28.     }
    29.  
    30.     void Update ()
    31.     {
    32.     }
    33.  
    34.     void FixedUpdate ()
    35.     {
    36.         float moveHorizontal = Input.GetAxis ("Horizontal");
    37.         float moveVertical = Input.GetAxis ("Vertical");
    38.  
    39.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    40.  
    41.         GetComponent<Rigidbody>().AddForce  (movement * speed);
    42.  
    43.     }  
    44.  
    45.  
    46.    
    47.     public void OnTriggerEnter(Collider other)
    48.     {
    49.            if (other.gameObject.CompareTag ("PickUp")) {
    50.  
    51.             PlaySound (1);
    52.             other.gameObject.SetActive (false);
    53.             count = count + 1;
    54.             SetCountText ();
    55.             }
    56.  
    57.         if (other.gameObject.CompareTag ("Wall"))
    58.         {
    59.             PlaySound (0);
    60.             other.gameObject.SetActive (false);
    61.             count = count - 1;
    62.             SetCountText ();
    63.         }
    64.     }
    65.  
    66.     public void PlaySound(int clip)
    67.     {
    68.         GetComponent<AudioSource>().clip = audioClip[clip];
    69.         GetComponent<AudioSource>().Play ();
    70.  
    71.     }
    72.     public void SetCountText ()
    73.     {
    74.  
    75.         CountText.text = "Points: " + count.ToString ();
    76.         if (count >= 6)
    77.         {
    78.             //Application.LoadLevel ("Level2");
    79.             Application.LoadLevel(Application.loadedLevel + 1);
    80.         }
    81.         if (count <= -2)
    82.         {
    83.             ThePlayer.gameObject.SetActive (false);
    84.             TheDeathScreen.gameObject.SetActive (true);
    85.             tip.gameObject.SetActive (false);
    86.         }
    87.     }
    88. }
    89.  
     
  2. Sose

    Sose

    Joined:
    Dec 10, 2015
    Posts:
    27
    Not sure I understand everything that's going on here.. But since your int count is public and static, you should be able to access it from everywhere by simply writing PlayerController.count

    Code (csharp):
    1.  
    2. private int a; // only accessible from within the class
    3. private static int b; // only accessible from within the class, shared between every instance of the class
    4.  
    5. public int c; // anyone who has access to the object can access this property
    6. public static int d; // can be accessed from anywhere by ClassName.d; shared between every instance
    7.  
    I'm not sure if your logic will still work, because you are apparently checking the achievement unlocks in a Start() method, so your PlayerController.count will be exactly 0 or 6 anyways, since you advance the scene when it's 6 and reset it to 0 on PlayerController's Start() method. I'm assuming that the Start() method is called when a new scene is loaded but I'm not 100% sure if that's the case
     
  3. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Why not just tag player controller object as "playercontroller" and use GameObject.FindObjectByTag("playercontroller").GetComponent<playercontroller>()

    Or in API just do PlayerController.count since its static
     
  4. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    PlayerController.count doesn't work, and I don't understand- where do I put in GameObject.FindObjectByTag("playercontroller").GetComponent<playercontroller>()
     
  5. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    I haven't made level 5, so as I said, I can't use (SceneManager.GetActiveScene().name=="Level5") .
     
  6. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    PlayerController.cs
    Code (csharp):
    1.  
    2. PlayerController pControl = GameObject.FindGameObjectByTag("playercontroller").GetComponent<PlayerController>();
    3.  
    4. int count = pControl.count;
    5.  
    Make sure you add a new tag to tags in designer and set the playercontroller GameObject's tag to playercontroller

    Edit sorry this is in api script not playercontroller.cs
     
  7. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    I'm getting two errors:
    Assets/Scripts/APIScript.cs(8,48): error CS0117: `UnityEngine.GameObject' does not contain a definition for `FindGameObjectByTag'

    Assets/Scripts/APIScript.cs(10,21): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `APIScript.pControl'
     
  8. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    I'm sorry it's findgameobjectwithtag, I'm still fairly new to unity and typing on iPad
     
  9. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    It doesn't matter, thanks for taking your time and helping me anyways :)
    I'm getting Another error now(Yes, I did type in FindGameObjectWithTag)
    Assets/Scripts/APIScript.cs(10,21): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `APIScript.pControl'
    I'm sorry, I'm new to scripting.
     
  10. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Remove static keyword from your player class count variable

    And put the find method inside your start method in Api script
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Since you've made your 'count' variable public and static, You can access it from anywhere with just this:
    Code (csharp):
    1. if (PlayerController.count ==6) {
     
  12. Nikola310

    Nikola310

    Joined:
    Oct 24, 2015
    Posts:
    35
    Hmm..Thanks for your answers, I'm getting no errors in the console but the gamejolt servers are offline (for me at least) so I'll see if it works tomorrow.