Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Xelioth · Apr 22, 2014 at 05:04 AM · multiplayerprefabstagfindwithtaghoming

Homing Missile target with "Find Objects With Tag" not working?

I'm trying to make a homing missile. The way I have the spells in my game, when you press the fire button, it instantiates a prefab. That prefab then does whatever the spell or ability is supposed to do, and then destroys itself. Each prefab is generic and every player casts the same prefabs. The prefabs are also fired from a "Gun" null object that's parented to the player, so there's a slight disconnect in the hierarchy. To help with that, the "Cast" button creates each prefab as a child of the "Gun" null object.

Hopefully that makes sense. The reason that's all relevant, is because when this homing missile is fired, it has to find a target. It sifts through the "Find Objects with tag" code that unity script reference offers, and SHOULD then assign the "closest" to the "target". But it doesn't. I've tested this code with a hard-coded target, and it works just fine. I've tested it with a more basic "just find anything with the player tag" and it works there too. It's just the script reference "FindClosestPlayer" function that doesn't seem to be working.

Or maybe I'm just trying to interact with it the wrong way. Either way, I keep getting the following error:

"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) Chase.Update () (at Assets/Other/Spells/Chase.js:23)"

And I'm stumped at this point. Please peruse my code and tell me exactly how and why I am an illiterate moron.

 var target : GameObject; //the missile's target
 var moveSpeed = 3; //move speed
 var rotationSpeed = 3; //speed of turning
 var lifeTime : float;
  
 var myTransform : Transform; //current transform data of this enemy
  
 function Awake()
 {
     myTransform = transform; //cache transform data for easy access/preformance
 }
  
 function Start()
 {
     FindClosestPlayer();
     
     Destroy(gameObject, lifeTime);
 }
  
 function Update () {
     //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
  
     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
  
  
 }
 
 // FIND THE CLOSEST PLAYER OTHER THAN THE CURRENT PLAYER 
 function FindClosestPlayer() : GameObject { 
     // Find all game objects with tag Player 
     var gos : GameObject[]; 
     gos = GameObject.FindGameObjectsWithTag("Player"); 
     var distance = Mathf.Infinity; 
     var position = transform.position; 
     // Iterate through them and find the closest one 
     for (var players : GameObject in gos) {
      if (players == gameObject.transform.root) continue; 
        var diff = (players.transform.position - position); 
        var curDistance = diff.sqrMagnitude; 
        if (curDistance < distance) {  
          closestPlayer = players;
          distance = curDistance; 
          }
        
     } 
     return closestPlayer; 
     target = closestPlayer;
 }
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

2 Replies

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

Answer by Griffo · Apr 22, 2014 at 06:34 AM

is it because you return before setting target = closestPlayer;

should it not be ..

 target = closestPlayer;
 return closestPlayer;

also add

 target = FindClosestPlayer();

in Start()

There are two errors beside the above in the code line 22 should be

 Quaternion.LookRotation(target.transform.position - myTransform.position), rotationSpeed*Time.deltaTime);

and line 44 var has not been declared, so need to add to the top

 var closestPlayer : GameObject;

Then it should work.

Comment
Add comment · Show 8 · 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 Xelioth · Apr 22, 2014 at 04:41 PM 0
Share

Woo! That worked!

$$anonymous$$y exclusion code isn't working properly, line 40 if (players == gameObject.transform.root) continue;

Do you have any brilliant solutions for excluding anything along the hierarchy of the prefab? (Sorry to ask this one, I'm still new at this and I haven't found any examples of this sort of thing online)

avatar image Griffo · Apr 22, 2014 at 05:25 PM 0
Share

You could do something like

 if (players.tag == gameObject.transform.root.tag) continue;

so it will only target the Player.

Not sure what you'r trying to do? you say "exclude anything along the hierarchy of the prefab" if you'r firing a missile at the GameObject "Player" then anything child of the Player is going to get destroyed if you destroy the GameObject Player.

avatar image Xelioth · Apr 22, 2014 at 06:34 PM 0
Share

It's more that I have two players. Each with the same "player" tag. I want either one to be able to shoot the missile and have it target the other, but not itself.

avatar image Griffo · Apr 22, 2014 at 07:17 PM 0
Share

Why don't you tag them as "Player_01" and "Player_02" then change line 40 to

 if (players.tag != transform.root.tag) continue;


avatar image Xelioth · Apr 23, 2014 at 05:28 AM 0
Share

it'd work for now, but I'd eventually like to have things affect all players equally. And after playtesting, my friends are begging me to make it a 4 player game, so I'd like it to just have one tag to sift through. Find a player. Is it the nearest player, but not the player who cast this spell? ok, go after that player.

Show more comments
avatar image
0

Answer by getyour411 · Apr 22, 2014 at 05:21 AM

You only call FindClosestPlayer once in Start(), so if another player isn't found at Start(), closestPlayer is probably null which is why 23 breaks down. You'll need to move the call to FindClosestPlayer in a coroutine, invoke, update, etc. and you should test that closestPlayer is not null

Comment
Add comment · Show 1 · 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 Xelioth · Apr 22, 2014 at 05:36 AM 0
Share

I may just be tired from staring at this all day, but would it work if I set up in Update so that "if (target != null}" then it just runs "FindClosestPlayer ()" again?

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

21 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

Related Questions

Getting a Gameobject with a tag 2 Answers

How do I update player tilt in multiplayer? 0 Answers

How to manually handle multiplayer prefabs spawning with our own script ? 0 Answers

Attack enemies in order 1 Answer

some prefab collisions randomly stop working 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