Addforce is not working
I'm instantiating a ball at the start of the scene and I need to come that ball to the screen. But after instantiated it's not moving :( Please help. I'm new to unity.
var thePrefab:GameObject; function Start(){ var ball =Instantiate(thePrefab,transform.position,transform.rotation); ball.GetComponent.().AddForce(transform.forward*1000); }
Answer by Ali-hatem · Apr 15, 2016 at 10:10 AM
ball should have a Rigidbody attached to it then C# :
Rigidbody rb;
void Start(){
rb = thePrefab.GetComponent<Rigidbody>();
rb.AddForce(transform.forward*1000);
}
but i recommend to attach a script to ball that handle movement once it instantiated because in Example 1 you may get error : "object reference not set to instance of an object" & then we have to use GameObject.Find()
which loop throw all game objects in the scene to find the ball object so it more efficient & simpler ball script :
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
rb.AddForce(transform.forward*1000);
}
Did this. No luck ! :(
var thePrefab:GameObject; var rb:Rigidbody;
function Start(){ var ball =Instantiate(thePrefab,transform.position,transform.rotation); rb.GetComponent.().AddForce(transform.forward*1000); }
rb =thePrefab.GetComponent<Rigidbody>().AddForce(transform.forward*1000);
edited i thought you used a script for ball movement if not why you don't use