- Home /
scripting error
hi everyone i trying to my character shoot fireball but i got htis error
NullReferenceException: Object reference not set to an instance of an object this is my script
var speed : float = 3.0; var rotateSpeed : float = 3.0; var bullitPrefab: Transform;
function Update () { var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward var forward : Vector3 = transform.TransformDirection(Vector3.forward); var curSpeed : float = speed Input.GetAxis ("Vertical"); controller.SimpleMove(forward curSpeed);
if(Input.GetButtonDown("Jump")) { var bullit = Instantiate(bullitPrefab, GameObject.Find("SpawnPoint").Transfrom.position,Quaternion.identity);
} } @script RequireComponent(CharacterController)
Answer by aldonaletto · Sep 25, 2011 at 03:12 AM
There's a problem in this instruction:
Instantiate(bullitPrefab,GameObject.Find("SpawnPoint").Transfrom.position,...
There's no Transfrom component - it's .transform.position:
Instantiate(bullitPrefab,GameObject.Find("SpawnPoint").transform.position,...
Notice that transform must be in lower case, since it's a game object variable. Additionally, make sure that an empty object named "SpawnPoint" exists (that's where the bullets will be instantiated).
Thanks for co$$anonymous$$g our timberland online store! Just enjoy yourself here! As a developping company, timberland boots always can give us some surprise .I belive timberland shoes Sale can make your feet more comfortable, make your life more stylish!If you want to have a try ,just click here: discount timberland boots Free delivery
thanzx i fixed it but now my question is how can i make my fireball move
The best way is to set the rigidbody velocity. You can use the transform.forward vector, what will shoot the bullet horizontally in the direction the player is turned to:
// shoot the bullet at 10m/s
bullit.rigidbody.velocity = transform.forward * 10;
A better alternative is to use the camera forward direction, so you can control the elevation angle you're shooting - substitute transform.forward by Camera.main.transform.forward
Your answer
