• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by alextex · Aug 28, 2015 at 11:43 PM · object-reference-not-set

object reference not set to an instance error

hi I'm pretty new to Unity and I started to make a game, I was following many tutorial online, and tried to add a Boolean to start an animation for an ennemy but had a problem, this is the full script

using UnityEngine; using System.Collections; public class ennemi : MonoBehaviour { public Transform sightStart, sightEnd; public bool spotted = false; public GameObject arrow; public float speed = 1f;

 public Transform sightStart2, sightEnd2;
 public bool spotted2 = false;
 Animator anim;

 void start(){
     anim = GetComponent<Animator> ();
         }

 void Update () {

     Raycasting ();
     Raycasting2 ();
     Behaviours ();

     if (spotted2 == true) {
         anim.SetBool("attack", true);
     }
 }
 void Raycasting(){

     Debug.DrawLine(sightStart.position, sightEnd.position, Color.red);
     spotted = Physics.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));


 }
 void Raycasting2(){
     Debug.DrawLine(sightStart2.position, sightEnd2.position, Color.blue);
     spotted2 = Physics.Linecast (sightStart2.position, sightEnd2.position, 1 << LayerMask.NameToLayer ("Player"));
 
 
 }


 void Behaviours(){

     if (spotted == true) {
         arrow.SetActive(true);
         transform.Translate(Vector3.right * speed * Time.deltaTime);
     }
     else{
         arrow.SetActive(false);
     }
 }

     

}

and I get the error : Object reference not set to an instance of an object ennemi.Update () (at Assets/script/ennemi.cs:25)

and I dont know how to fix it, could you help me ?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cjdev · Aug 29, 2015 at 12:32 AM 0
Share

Your code isn't formatted exactly right, what line is 25 in your script?

The error means one of your variables isn't pointing to an object, maybe because you didn't set it in the inspector or forgot to assign it somewhere.

avatar image alextex · Aug 29, 2015 at 12:49 AM 0
Share

the line 25 start by this void Update () { Raycasting (); Raycasting2 (); Behaviours (); if (spotted2 == true) { anim.SetBool("attack", true); } }

avatar image cjdev · Aug 29, 2015 at 12:56 AM 0
Share

Those are multiple lines but do you have an Animator attached to your GameObject?

avatar image alextex · Aug 29, 2015 at 01:15 AM 0
Share

yes, I have an animator attached to my GameObject

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges