Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 TheMightyFlash · Apr 13, 2016 at 12:22 AM · erroraienemy

Enemy AI Error

Hey guys. I'm having a problem with my Enemy AI moving towards my player. It keeps saying "Object reference is not set to an instance of the Object". Think anyone can help me fix it? My code is this;

public class AI1 : MonoBehaviour {

 private Vector3 Player;
 private Vector2 Playerdirection;
 private float Xdif;
 private float Ydif;
 private float speed;

 void start () {
     speed = 10;
 }

 void Update () {
     Player = GameObject.FindWithTag("Player").transform.position;

     Xdif = Player.x - transform.position.x;
     Ydif = Player.y - transform.position.y;

     Playerdirection = new Vector2 (Xdif, Ydif);

     GetComponent<Rigidbody2D>().AddForce(Playerdirection.normalized * speed);



 }

}

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · Apr 14, 2016 at 01:47 AM

I deleted my answer because I saw right after posting it that @jgodfrey had basically posted the same thing my I only corrected the script. but @jgodfrey is correct that you really should be initializing those variable in Start() or Awake() like so

 private Vector3 player;
 private Vector2 playerdirection;
 private float xDif;
 private float yDif;
 private float speed;
 private Rigidbody2D rigidbody = null;
 
 void start ()
 {
     speed = 10;
 
     // only get the player once instead of every frame
     player = GameObject.FindWithTag("Player");
 
     // only get the rigidbody once instead of every frame
     rigidbody = GetComponent<Rigidbody2D>();
 
     System.String error = "";
     if (player == null)
     {
         error = "GameObject.FindWithTag(Player) == null, ";
     }
 
     if (rigidbody == null)
     {
         error += "GetComponent<Rigidbody2D>() == null";
     }
 
     // only log the error once instead of every frame
     if (error != "")
     {
         Debug.Log(error);
     }
 }
 
 void Update ()
 {
     if ((player != null) && (rigidbody != null))
     {
         Vector3 position = player.transform.position;
         xDif = player.x - transform.position.x;
         yDif = player.y - transform.position.y;
         playerdirection = new Vector2 (xDif, yDif);
         rigidbody.AddForce(playerdirection.normalized * speed);
     }
 }
 

Comment
Add comment · Share
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
0

Answer by jgodfrey · Apr 13, 2016 at 01:05 AM

Is the error here...

 Player = GameObject.FindWithTag(...)

or here...

 GetComponent<Rigidbody2D>()...

If the error is in the first one, do you have an in-game object tagged as "Player" (with the exact same spelling and character case)? If it's in the second one, does the object the above script is attached to have a Rigidbody2D component?

Regardless of the error, both of those lines are not very efficient and should not be done in Update() (you're looking that information up in every frame - and they won't change from frame to frame)...

You really should look those things up in Start() or Awake() once and cache a reference to them. Then, use that cached reference in Update - it'll be much more efficient.

Comment
Add comment · Share
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
0

Answer by TheMightyFlash · Apr 14, 2016 at 01:24 AM

Alright, so my problem was solved by the user @Mavina (Thank you very much!!!). Unfortunately, the answer was deleted, so I will put a copy of it into this comment! Thank you again @Mavina!! (Here's the fully fixed script!)

public class AI1 : MonoBehaviour {

 private Vector3 Player;
 private Vector2 Playerdirection;
 private float Xdif;
 private float Ydif;
 public float speed;

 void start()
 {
     speed = 10;
 }

 void Update()
 {
     if ((GameObject.FindWithTag("Player")) && (GetComponent<Rigidbody2D>()))
     {
         Player = GameObject.FindWithTag("Player").transform.position;
         Xdif = Player.x - transform.position.x;
         Ydif = Player.y - transform.position.y;
         Playerdirection = new Vector2(Xdif, Ydif);
         GetComponent<Rigidbody2D>().AddForce(Playerdirection.normalized * speed);
     }
     else
     {
         System.String error = "";
         if (GameObject.FindWithTag("Player") == null)
         {
             error = "GameObject.FindWithTag(Player) == null, ";
         }
         if (GetComponent<Rigidbody2D>() == null)
         {
             error += "GetComponent<Rigidbody2D>() == null";
         }
         Debug.Log(error);
     }
 }



}

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

85 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 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 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 avatar image

Related Questions

Enemy AI Script 1 Answer

Trouble making jumping spider enemies 0 Answers

Attaching objects to scripts without dragging and dropping 0 Answers

2D Enemy Ai 0 Answers

Trying to make an enemyAI and have a few issues could I get some help? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges