I'm currently working on a rock that moves in a random direction but I can't make it work. Here's the script: Code (CSharp): using UnityEngine; using System.Collections; public class RockController : MonoBehaviour { private Rigidbody myRigidbody; public int speed; // Use this for initialization void Start () { myRigidbody = GetComponent<Rigidbody>(); } // Update is called once per frame void Awake () { int x = Random.Range (-1, 1); int z = Random.Range (-1, 1); Vector3 direc = new Vector3 (x, 0f, z); myRigidbody.AddForce (direc * speed); } } And the error message:
The actual problem is that Awake is always called before Start. So you've got a slightly wrong order there.
So if I want to apply that force just once at the very start of the game, what should I use instead of Awake?
Code (CSharp): public class RockController : MonoBehaviour { private Rigidbody myRigidbody; public int speed; Vector3 direc; // Use this for initialization void Start () { myRigidbody = GetComponent<Rigidbody>(); int x = Random.Range (-1, 1); int z = Random.Range (-1, 1); direc = new Vector3 (x, 0f, z); } // FixedUpdate is called once per physics frame void FixedUpdate() {//ADD SPEED EVERY PHYSICS FRAME myRigidbody.AddForce (direc * speed); } } Look up the ForceMode variable for the AddForce method. also, if your drag is zero and you keep speeding it up, you're gonna have a bad day.