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
using UnityEngine;
using System.Collections;
public class HUDScript : MonoBehaviour {
float playerScore = 0f;
// Update is called once per frame
void Update () {
playerScore += Time.deltaTime;
}
public void IncreaseScore(int amount)
{
playerScore += amount;
}
void OnDisable()
{
PlayerPrefs.SetInt("Score",(int)(playerScore * 100));
}
void OnGUI()
{
GUI.Label (new Rect (10, 10, 100, 30), "Score: " + (int)(playerScore * 100));//pixle loacation, pixle location, pixle wide, pixle tall
}
}
Platformer2DUserControl
using UnityEngine;
using UnitySampleAssets.CrossPlatformInput;
namespace UnitySampleAssets._2D
{
[RequireComponent(typeof (PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
private PlatformerCharacter2D character;
public float score;
private bool jump;
private void Awake()
{
character = GetComponent<PlatformerCharacter2D>();
}
private void Update()
{
if(!jump)
// Read the jump input in Update so button presses aren't missed.
jump = CrossPlatformInputManager.GetButtonDown("Jump");
score = HUDScript.playerScore;
}
private void FixedUpdate()
{
// Read the inputs.
//bool crouch = Input.GetKey(KeyCode.LeftControl);
//float h = CrossPlatformInputManager.GetAxis("Horizontal");
// Pass all parameters to the character control script.
character.Move(1f + score/100, false, jump);//character.Move(h, crouch, jump);
jump = false;
}
}
}