Well actually it seems i can(Unity console doesn’t trow any errors) but the code that does actually something with that variable doesn’t work.I have a script called HUDScript and in it the float variable scorePlayer.I declared them both public in that script.Now i have another script(CameraRunnerScript) which constantly moves the camera to the right(platformer/infinite runner).I want to increase the moving speed of the camera once the score is above a number(for now 300).I called the playerScore variable from the HUDScript in the CameraRunnerScript but i believe i’m not doing it wright. So here’s the code:
public class CameraRunnerScript : MonoBehaviour {
float playerScore;
public Transform player;
// Use this for initialization
void Start () {
playerScore = GameObject.Find ("Main Camera").GetComponent<HUDScript>().playerScore;
}
// Update is called once per frame
void Update () {
transform.position = new Vector3 (transform.position.x + 0.15f, 0, -10);
if(Time.timeScale == 0)
{
transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
}
if ((int)(playerScore) >= 300)
{
transform.position = new Vector3 (transform.position.x + 0.5f, 0, -10);
}
}
}
That last if condition is never tested since it doesn’t do anything. It doesn’t recognize the playerScore variable from the HUDScript? If so why doesn’t the console give me any errors?
Thank you!
It’s normal, in the start function, your are making a copy of the value of HUDScript.playerScore, and your are not getting a reference on it.
I would advise you to check the if( playerScore >= 300f ) ...
condition inside the HUDScript script. In the HUDScript, add a public variable of type CameraRunnerScript and drag your camera in the field which appeared in the inspector.
When the condition is verified, call the function IncreaseSpeed() of your CameraRunnerScript defined as follow :
public void IncreaseSpeed()
{
if( currentSpeedIndex < Speeds.Length - 1 )
currentSpeedIndex++ ;
}
The variables involved are defined as follow :
// Add the different speeds directly from the inspector
public Vector3[] Speeds ;
// Index of the speed
private int currentSpeedIndex = 0
// Current speed of the camera = Speeds[currentSpeedIndex]
In your Update function, you will just have to write this :
if(Time.timeScale == 0)
{
transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
}
else
{
transform.Translate( Speeds[currentSpeedIndex] ) ;
}
Full C# script :
public class CameraRunnerScript : MonoBehaviour
{
public Transform player;
// Add the different speeds directly from the inspector
public Vector3[] Speeds ;
// Index of the speed
private int currentSpeedIndex = 0 ;
// Update is called once per frame
void Update () {
transform.position = new Vector3 (transform.position.x + 0.15f, 0, -10);
if(Time.timeScale == 0)
{
transform.position = new Vector3 (transform.position.x-0.15f , 0, -10);
}
else
{
transform.Translate( Speeds[currentSpeedIndex] ) ;
}
}
// Called by HUDScript every time the player score is above a new step
public void IncreaseSpeed()
{
if( currentSpeedIndex < Speeds.Length - 1 )
currentSpeedIndex++ ;
}
}
public class HUDScript : MonoBehaviour
{
// Drag the camera holding the CameraRunnerScript from the inspector
public CameraRunnerScript CameraRunnerScript ;
public float playerScore ;
// Update is called once per frame
void Update () {
...
if( playerScore > 300 )
{
CameraRunnerScript.IncreaseSpeed() ;
}
...
}
}