- Home /
Assignment working in Update() but not in Start()?
I'm trying to access my Player object's Rigidbody2D so I can set its velocity based on controller input. To do this, I was assigning the Rigidbody2D to a variable "playerRB", using the following line of code:
Rigidbody2D playerRB = this.gameObject.GetComponent<Rigidbody2D> ();
After some trial and error, I found that my approach works just fine but only if I place that assignment statement in the Update() function. I'd rather place it in the Start() function, because wouldn't that be more efficient? As I understand it, it shouldn't need to reassign that value every frame, and doing so is just wasting CPU time. Maybe I'm wrong, though, and that assignment does need to happen every frame.
Is it not working in Start() because the object isn't yet initialized so trying to access the GameObject at that point doesn't work? If so, can I create a separate function to handle the assignment of playerRB, and set that function to run only once immediately after the object is initialized?
Full code below. Note that the code pasted below is working as intended, I just want to know if it's possible to move the assignment of playerRB from Update() to Start() to be more efficient. I'm new to this, so any other comments, criticisms, suggestions or improvements are also welcome.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float Speed = 1.0f;
float velx = 0.0f;
float vely = 0.0f;
public PlayerProjectile weapon;
Rigidbody2D playerRB;
// Use this for initialization
void Start ()
{
//The line below doesn't work when placed here in Start(), but it works when it's placed in Update ().
//Rigidbody2D playerRB = this.gameObject.GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update ()
{
//The line below works when placed in Update(), but I'd prefer not to call it every frame.
Rigidbody2D playerRB = this.gameObject.GetComponent<Rigidbody2D> ();
ControllerInput ();
Vector2 movement = new Vector2 (velx, vely);
if (playerRB == null)
{
Debug.Log ("playerRB is null!");
}
playerRB.velocity = (movement * Time.deltaTime * Speed);
}
void ControllerInput()
{
velx = Input.GetAxis ("LeftStick_X");
vely = Input.GetAxis ("LeftStick_Y");
if (Input.GetButtonDown ("Y"))
{
weapon = Resources.Load<PlayerProjectile>("Prefabs/Weapons/FastZombieCannon");
}
if (Input.GetAxis ("RightTrigger") >= 0.5f)
{
Fire();
}
}
void Fire()
{
float playerDirection = Mathf.Atan2 (Input.GetAxis ("RightStick_Y"), Input.GetAxis ("RightStick_X"));
PlayerProjectile playerProj = Instantiate (weapon, new Vector3 (this.gameObject.transform.position.x + ( Mathf.Cos (playerDirection) * 1.0f ), this.gameObject.transform.position.y + ( Mathf.Sin (playerDirection) * 1.0f ), 0), Quaternion.identity) as PlayerProjectile;
//playerProj.transform.rotation = Quaternion.Euler (playerDirection, 0, 0);
Rigidbody2D projRB = playerProj.gameObject.GetComponent<Rigidbody2D> ();
if (projRB == null)
{
Debug.Log ("playerProj Rigidbody2D not found!");
}
projRB.velocity = new Vector2 ( ( Mathf.Cos (playerDirection) * playerProj.ProjectileSpeed ) , ( Mathf.Sin (playerDirection) * playerProj.ProjectileSpeed ));
}
}
Answer by DoTA_KAMIKADzE · May 02, 2015 at 12:01 AM
It doesn't work because you make a variable inside that function with name of your script's variable instead of assigning the returned value to it. Basically just change this:
Rigidbody2D playerRB = this.gameObject.GetComponent<Rigidbody2D>();
to this:
playerRB = GetComponent<Rigidbody2D>();
Also as you probably noticed above^ you don't need to explicitly say this.gameObject.
P.S. Also if your Rigidbody2D component is there before your script is enabled you can get it on Awake:
private void Awake()
{
playerRB = GetComponent<Rigidbody2D>();
}
You can read the difference between Awake and Start by pressing those links or for even better understanding visit Lifecycle page.
This worked perfectly, thanks a ton! The difference between "assigning a value to the existing variable" and "creating a new local variable with the same name" would never have occurred to me. Scope is one of those things that is still hard for me to fully wrap my head around, but now that you point it out it makes perfect sense.
Your answer has actually helped me reach an "Aha!" moment on a couple other issues I've been working on as well. (I get the feeling that happens a lot in program$$anonymous$$g =p)
Thanks again!