- Home /
Duplicate Question
Object reference not set to an instance of an object?
Hi There! So, today I am having a serious problem, I have a script (Which is below) that whenever I attempt to make it so that my object attacks my other object, it just say "Object reference not set to an instance of an object" in the console and the object doesn't even move! I've been having this problem and I know its something about "null" but I have no idea what that is. Noted that I am not a pro at scripting so go easy on me but still give me some good feedback! Anyways here is the script:
var Base = GameObject.Find("Red Base (Clone)");
var moveSpeed = 5.0;
var Damping = 6.0;
function AttackBase () {
var rotation = Quaternion.LookRotation(Base.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation2, Time.deltaTime * Damping);
transform.position = Vector3.MoveTowards(transform.position, Base.position, moveSpeed * Time.deltaTime);
}
function Update() {
AttackBase();
}
You need to declare base as:
var Base : GameObject;
...and you need to initialize it in Start() (if it exists before the script is executed):
function Start() {
Base = GameObject.Find("Red Base (Clone)");
}
If it does not exist when this script is first run, then you will need to do it in Update(), but I suggest you do it by tag rather than by name.
Follow this Question
Related Questions
Check if GameObject.Find returns null 1 Answer
check if an instantiated opject is null 1 Answer
Issue with an object rotating strangely 2 Answers
Rigidbody Disable Velocity/Movement? 1 Answer
Position and rotation of a game object acting wired 0 Answers