So im giving the first steps in unity and i have one big doubht wich is realy making me frustrated, i just want to access/change values from one script to another…
What i mean is i have for example two scripts, one for player movement and another for player collision, well how can i change, lets say player velocity but in the player collision script? (player velocity is in player movement script)
To access a script that is one the same gameobject you can simply do this
private PlayerCollision playerCollision; // SCRIPTNAME reference
void Start()
{
playerCollision = GetComponent<PlayerCollision>(); // retireves the component on the same gameobject
}
So when you reference playerCollision now you can now retrieve all public variables/functions from this script, its the same purpose on if you wished to reference playermovement in playercollision
Well i tried the GetComponent, wich would get the Script, but i still dont quite get on how i would change it there…
here are the 2 scripts:
public class PlayerMovement : MonoBehaviour {
public Rigidbody rb;
public float cubeVelocityZ = 1500f;
public float cubeVelocityX = 800f;
// Update is called once per frame
void FixedUpdate ()
{
rb.AddForce(0, 0,cubeVelocityZ * Time.deltaTime); // Change this Settings
double halfScreen = Screen.width / 2.0;
if (Input.GetMouseButton (0) & Input.mousePosition.x>halfScreen)
{
rb.AddForce (cubeVelocityX * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetMouseButton (0) & Input.mousePosition.x<halfScreen)
{
rb.AddForce (-cubeVelocityX * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager> ().EndGame ();
}
}
And the player Collision:
public class PlayerCollision : MonoBehaviour {
public float slowDown =8f;
bool waitDelayTime = false;
public GameManager gameManager;
IEnumerator OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle" & waitDelayTime == false)
{
Time.timeScale = 1f / slowDown; // Change the Settings in Here
waitDelayTime = true;
Time.fixedDeltaTime = Time.fixedDeltaTime / slowDown;
yield return new WaitForSeconds (0.1f);
Time.timeScale = 1f;
Time.fixedDeltaTime = Time.fixedDeltaTime * slowDown;
waitDelayTime = false;
//FindObjectOfType<GameManager>().EndGame();
}
}
Ok just say i want to change the rb.AddForce(0, 0,cubeVelocityZ * Time.deltaTime) wich is in the player movement and change it inside the if statment in player collisison: if (collisionInfo.collider.tag == “Obstacle” & waitDelayTime == false)
This seems quite simple so im not doing something rigth here…
You can change values of variables. However, you’re not going to directly modify lines of code, if that is what you are asking. If you needed that, you could set up bools to avoid running some code and running others instead.
Basically, both the lines you want to change, just make sure any values you want to modify are public variables that you can access outside the script.
There is nothing wrong with static variables, however, I would not tell a person to just make a variable static if they don’t even know how to use GetComponent as this may lead to the idea of using static everywhere without knowing what static actually does.
I was thinking if they don’t know how to use GetComponent, then using a static variable would get the job done. When starting out, I would like to have known all options of accessing variables from other scripts. But yes, I wouldn’t recommend just using statics everywhere. I would prefer a project design that doesn’t need many script to script variable references, and lets each script manage it’s own members.
Thanks for the help guys, i do know how to getComponent and i use to get values i need to change but in here i dont know how i can get to the rb.AddForce(0, 0,cubeVelocityZ * Time.deltaTime) wich is in the player movement fixed update, i want to change it from the player collision script…
Also DougMcFarlane why would i need to make the variable Static?would it not work if i just made it public and acess the way you posted?
Like:
public double playerVelocity = 1.0;
PlayerMovement.playerVelocity = 2.0;
If it’s static, you can access it without an instance of that class. If it’s like you have it, it doesn’t work. You’d have to create an instance of that class and access the variable from the instance.
For scripts that you know will ONLY be on one gameObject (like PlayerMovement script on a Player object), you could also make a Singleton style script by having a static ‘Instance’ variable declared. For example:
public static Instance;
Then instantiate it on the gameObject’s Awake method like this:
void Awake() {
Instance = this;
}
Now, from other scripts you have access to all Public (non static) variables and methods in that script, like this: