I want to access to a variable of the script “ExampleScript”, which is put in a gameObject in the scene, called “player”, and using it in the script “AccessVariabileScript”.
This is the Player Script where there is the variable MyMoreSpeed:
using UnityEngine;
using System.Collections;
public class ExampleScript : MonoBehaviour {
//SpeedBall
public int myMoreSpeed = 0;
void OnTriggerEnter(Collider other){
if(other.gameObject.name == "MoreSpeed"){
myMoreSpeed++;
}
}
Now, in another Script I want to access to the variable myMoreSpeed and, if this variable is >= 1 do somthing, in that case by touching the screen of an touchscreen and, if the myMoreSpeed is >= 1, the speed change.
I tried to do a c# code by myself. But it was useless.
This is the script:
using UnityEngine;
using System.Collections;
public class AccessVariableScript : MonoBehaviour {
public float speed = 1; //<-- Normal speed
public float speedUP = 2; //<-- If all the conditions are true, speed = speedUP
public float speedA = 1;
void Awake(){
GameObject myPlayer = GameObject.Find("Player"); //<-- I tried to find the gameObject in the scene
SpeedA = myPlayer.GetComponent<ExampleScript>().myMoreSpeed; //<-- ????
}
void Update () {
//Simple movement
transform.Translate(0,-speed,0);
//MoreSpeed. In this part of code I don't undertand why if I don't put "SpeedA >=1" works, but if I put it , doesn't work.
if( SpeedA >= 1 /* I don't know if this is correct */ & Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){
speed = SpeedUP;
}
}
Unity3d doesn’t report any error but the script doesn’t work as I want.
Another question I want to ask you is how can I decrease The variable myMoreSpeed by 1 point, when the player touch the screen of an Touchscreen?
Thanks for the attention and if you found any typos I apologize, I’m Italian and I don’t speak english very well.