Object reference not set to an instance of an object
I'm new in unity and my code showing me this error
NullReferenceException: Object reference not set to an instance of an object ShootScript.Update () (at Assets/Scripts/ShootScript.cs:30)
please tell me how to fix this.
using UnityEngine;
using System.Collections;
public class ShootScript : MonoBehaviour {
Rigidbody arrowRigidBody;
public int shootSpeed;
public bool shootStart;
public float xForce;
public float yForce;
public float movementSpeed;
public float distance;
void Start()
{
arrowRigidBody = GetComponent<Rigidbody> ();
}
void Update()
{
if (shootStart)
{
//float xtemp = Input.acceleration.x*xForce;
//float ytemp = Input.acceleration.y*yForce;
// ArrowRigidBody.velocity = new Vector3 (-xtemp,-ytemp,shootSpeed);
arrowRigidBody. transform.position = transform.position + Camera .main.transform.forward * distance * Time.deltaTime;
}
}
public void stop()
{
arrowRigidBody.velocity=new Vector3 (0,0,0);
shootStart = false;
}
public void setBoolea()
{
shootStart = true;
}
}
your shootStart is not initialised.. In the Start (), function just give shootStart = false; (or whatever necessary)
Answer by Sinnii · Sep 16, 2016 at 12:19 PM
There are two alternatives.
Either arrowRigidBody is null, in which case your GameObject doesn't have a RigidBody component on it.
Or, Camera.main is null, in which case your scene doesn't have a Camera with the MainCamera tag on it.
Your answer
Follow this Question
Related Questions
Create group people, Select one or more at random. 1 Answer
CameraController (object reference not set to an instance of an object) 1 Answer
NullReferenceException: Object reference not set to an instance of an object 3 Answers
How do I prevent the destroying of all instances of an object in Unity? 0 Answers
Object reference not set to an instance of an object 0 Answers