• 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 /
avatar image
0
Question by Gizandjess · Aug 03, 2012 at 08:21 PM · enemytag

Using tags to set to an instance of an object?

Hi. I have a basic enemy AI which follows the player. I borrowed this script from some place in Unity answers and then adapted it to my own use. I ran into a problem where the enemy kept walking into the player, so I solved that. But then, because I use a large group of enemies, I found another problem where the enemies were pushing and shoving to get to the player, causing some unwanted side effects. I thought I would solve this by using the same way I used to make them stop in front of the player, but I have got this error:

NullReferenceException: Object reference not set to an instance of an object Enemyfollow.Update () (at Assets/Enemyfollow.js:18)

The script is as follows:

 var distance;
 var otherEnemies;
 var distanceEnemies;
 var target : Transform;    
 var lookAtDistance = 15.0;
 var attackRange = 10.0;
 var stopMovePlayer = 1.0;
 var moveSpeed = 5.0;
 var stopMoveEnemies = 1.0;
 
   

 function Update () 
 {
 distance = Vector3.Distance(target.position, transform.position);
 otherEnemies = GameObject.FindGameObjectsWithTag ("Enemy");
 distanceEnemy = Vector3.Distance(otherEnemies.position, transform.position);
 
 if(distance < lookAtDistance)
 {
 lookAt ();
 } 
 if(distance < attackRange && distance > stopMovePlayer && distanceEnemies > stopMoveEnemies)
 {
  attack ();
 }
 }

There is more after that, but that has nothing to do with the problem and works perfectly.

The line where it is going wrong is here:

 distanceEnemy = Vector3.Distance(otherEnemies.position, transform.position);

I assume it is because the FindGameObjectWithTag option doesn't set to an instance of an object or something, so it can't calculate the distance between the enemies, but I don't know how to solve this.

Help would be appreciated, in any form.

Comment
Add comment · Show 2
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 OperationDogBird · Aug 03, 2012 at 09:12 PM 0
Share

otherEnemies is an array? If this is so you should use FindGameObjects ins$$anonymous$$d of FindGameObject since you want all the objects of this tag. You would also need to cycle through all the enemies in that array using a for/for each loop.

You are right that other Enemies is null when the line is called. You could add a check like

 if(otherEnemies)//check distance

also you should run Find functions in Start() rather than Update if possible.

avatar image Gizandjess · Aug 04, 2012 at 05:11 PM 0
Share

Ok, thanks. However, I am starting to think that my original way to tackle this problem won't work. Is there a way that I can solve this without going through a lot of code, or will I have to make do?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

how do i make an Enemy take damage from prefab bullet clone? 1 Answer

Change something in this script 2 Answers

Enemy Tag in Range 1 Answer

How to make an enemy change tags after death. 3 Answers

Only damage one object with "Enemy" tag 2 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