- Home /
Assigned variable
Hi I can figure out what is wrong on this script:
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 2f;
void Update () {
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h, v, 0);
if(Input.GetButtonUp("Fire1"))
{
Rigidbody instance = Instantiate(bullet, transform.position, transform.rotation)as Rigidbody;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
}
}
}
I geting this:
You probably need to assign the bullet variable of the Shooter script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:74) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineObject.cs:84) Shooter.Update () (at Assets/Shooter.cs:21)UnassignedReferenceException: The variable bullet of Shooter has not been assigned.
And have you assigned the bullet variable in the inspector?
I noticed that the message shows up when the object touches another object. When they do not it does not affect this message ... weird.
Answer by Kiwasi · Sep 16, 2014 at 07:18 PM
If you have the error only showing up after a collision then there is probably something in OnCollisionXXX that is destroying or breaking your connection to the bullet variable.
I added two of the same scripts to the camera and to the prefab. Resolved, thank you for your interest :).
Your answer

Follow this Question
Related Questions
Gui in Gui Not Working ? 1 Answer
Pause Menu Help? 2 Answers
Shoot direction 1 Answer
Unity3D Crashes when baking terrain 1 Answer
Monodevelope auto complete problem? 1 Answer