Search Unity

Having trouble getting score value from other script.

Discussion in 'Scripting' started by Brandon-Keeler, Mar 30, 2015.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    So I have a script attached to the main camera called "HUDScript", inside that script it increments my score based on how much time has passed. I have another script attached to my player called "Platformer2DUserControl", and i want to get the score from the other script because im going to use that to slowly increment my speed based on that value.

    HUDScript
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HUDScript : MonoBehaviour {
    5.  
    6.     float playerScore = 0f;
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.         playerScore += Time.deltaTime;
    11.     }
    12.  
    13.     public void IncreaseScore(int amount)
    14.     {
    15.         playerScore += amount;
    16.     }
    17.  
    18.     void OnDisable()
    19.     {
    20.         PlayerPrefs.SetInt("Score",(int)(playerScore * 100));
    21.     }
    22.  
    23.     void OnGUI()
    24.     {
    25.         GUI.Label (new Rect (10, 10, 100, 30), "Score: " + (int)(playerScore * 100));//pixle loacation, pixle location, pixle wide, pixle tall
    26.     }
    27. }
    Platformer2DUserControl
    Code (CSharp):
    1. using UnityEngine;
    2. using UnitySampleAssets.CrossPlatformInput;
    3.  
    4. namespace UnitySampleAssets._2D
    5. {
    6.  
    7.     [RequireComponent(typeof (PlatformerCharacter2D))]
    8.     public class Platformer2DUserControl : MonoBehaviour
    9.     {
    10.         private PlatformerCharacter2D character;
    11.         public float score;
    12.      
    13.  
    14.         private bool jump;
    15.  
    16.         private void Awake()
    17.         {
    18.             character = GetComponent<PlatformerCharacter2D>();
    19.         }
    20.  
    21.         private void Update()
    22.         {
    23.             if(!jump)
    24.             // Read the jump input in Update so button presses aren't missed.
    25.             jump = CrossPlatformInputManager.GetButtonDown("Jump");
    26.             score = HUDScript.playerScore;
    27.         }
    28.  
    29.         private void FixedUpdate()
    30.         {
    31.             // Read the inputs.
    32.             //bool crouch = Input.GetKey(KeyCode.LeftControl);
    33.             //float h = CrossPlatformInputManager.GetAxis("Horizontal");
    34.      
    35.          
    36.             // Pass all parameters to the character control script.
    37.             character.Move(1f + score/100, false, jump);//character.Move(h, crouch, jump);
    38.             jump = false;
    39.         }
    40.     }
    41. }
     
    Last edited: Mar 30, 2015
  2. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I tried many ways but every single one gives me an error in a compiler, if you can help i would be greatly appreciated.
     
  3. Mitch_AWeekAgo

    Mitch_AWeekAgo

    Joined:
    Oct 19, 2014
    Posts:
    22
    What does the error says? It might be HUDScript from Platformer2DUserControl no?
    Also playerScore private, unreachable?
     
  4. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    Its generally along the lines of this (this is my current builds error...)

    Assets/__MyScripts/Scripts/Platformer2DUserControl.cs(26,43): error CS0122: `HUDScript.playerScore' is inaccessible due to its protection level
     
  5. Mitch_AWeekAgo

    Mitch_AWeekAgo

    Joined:
    Oct 19, 2014
    Posts:
    22
    I see, make playerScore public or make accessors for him.
    Then in Platformer2DUserControl you need a object of type HudScript to access playerScore.
    Try something like this:

    GameObject objHud;
    HudScript hudScript;

    In Start:
    if(GameObject.FindObjectOfType<HudScript>())
    {
    objHud = GameObject.Find("nameOfHudScriptObjectInScene");
    hudScript = objHud.GetComponent<HudScript>();
    }

    In Update:
    score = hudScript.playerScore;

    another way:

    //This will be exposed in the expector, drag/drop the object that have HudScript in it
    public HudScript hudScript;

    In Update:
    score = hudScript.playerScore;

    This will help you:http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html