- Home /
bullet issue
hay guys umm ive got an error with my code but ive been playing with it and i dnt quite understand what wrong :( its say the prefab i want the instantiate is null
var bullet:Rigidbody;
var bulletspeed:int;
function Start () {
}
function Update () {
if (Input.GetButtonDown("Jump"))
{
if (Input.GetKeyDown(KeyCode.Space)){
var bullet: Rigidbody;
bullet = Instantiate(bullet,transform.position,transform.rotation);
}
}
}
Answer by AlucardJay · Oct 25, 2012 at 12:19 AM
no, this is a mess.
here's the script, fixed with notes. Make sure you read it and understand what is happening :
// this is the bullet that is copied and cloned
// it need to be a prefab with a rigidbody
// it needs to then be dropped here in the Inspector
var bulletPrefab : Rigidbody;
// this is the force applied when the bullet is instantiated
var initialForce : float = 200.0;
function Start () {
}
function Update()
{
// check if Jump OR Space has been pressed
// OR is written like this ||
// e.g. if ( something == 1 || anotherthing == 2 )
if ( Input.GetButtonDown("Jump") || (Input.GetKeyDown(KeyCode.Space) )
{
var bulletClone : Rigidbody; // this is the clone of the bullet
bulletClone = Instantiate( bulletPrefab, transform.position, transform.rotation );
// push the bullet when it is created ** note ForceMode.Impulse
bulletClone.AddForce( bulletClone.transform.forward * initialForce, ForceMode.Impulse );
}
}
I suggest you follow this video series, they are only about 5 mins each, and a great start to learning how to use Unity, components and scripts.
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/essential-skills/
Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/beginner/
this is the YouTube link for the above as one playlist : http://www.youtube.com/watch?v=-oXYHNSmTxg&list=PL27B696FB515608D2&feature=plcp
the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials
A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner