- Home /
Getting variables from one class to another
Hello everyone, I've recently delved into Unity as a part of my class, we have been told to recreate a retro game throughout this month and the next.
HOWEVER!
I've been stuck for quite a while on how to edit a variable in another class then the one you're currently working in.
Normally I'd write "Class class;" and then have access to it just fine, however when ever i try and do this, it returns everything in my class as null.
How do i avoid this?
Heres the classes I've been toying around with:
public Rigidbody bullet;
public bool canMove = false;
public float power = 1500;
public float moveSpeed = 5.0f;
// Use this for initialization
void Start ()
{
//joint.connectedBody = GameObject.Find("physics_body").rigidbody;
bullet = GameObject.Find("Cube").rigidbody;
}
// Update is called once per frame
void Update ()
{
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h, v, 0);
if(canMove == true)
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
}
void Shoot()
{
Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
This code basically just allows a player to choose in front of him/her, however since I've been trying to make a menu, this needs to be "paused" when ever the player is on the menu.
So i figured i could just do this:
bool canMove = false;
ShootBullet shoot;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(canMove == true)
{
shoot.canMove = true;
}
if(Input.GetKeyDown(KeyCode.B))
{
canMove = true;
}
}
But no, it just returns everything as null.
Hopefully someone can help me and point out where I've gone wrong.
Thanks.
Answer by skalev · Sep 11, 2012 at 04:04 PM
If you want to pause the game, set Time.timeScale = 0; it will pause the game and not the gui.
However, make sure that you account for that in your scripts, and in Update() check if time.timeScale == 0, and don't do any processing if that is the case.
Thank you, thought it was like in XNA, so if you disrupted the timer, it would stop the entire game.
However I'd still like to know what i do wrong with calling the bools. I could just be a giant idiot and not see it myself.
It looks like you are not assigning anything to the shoot variable. so it makes sense that it is null.
Your answer
Follow this Question
Related Questions
Why does IsSleeping() keep returning false? (Billiard Logic) 0 Answers
Desync between host and client. 0 Answers
GameObject variable doesn't get filled in 0 Answers
Accessing RigidbodyFirstPersonController 0 Answers
How can I define a joint for my ragdoll? 0 Answers