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 Nephxio · Dec 26, 2013 at 04:24 AM · c#instantiation

(C#)Tracking Instantiated Objects Globally

I think I am misunderstanding the difference/relationship between GameObject and Transform types.

Right now, I have a spawner prefab that uses the following code to spawn enemies:

     public void spawnEnemy()
     {
         var gameController = GameObject.FindWithTag ("GameController");
                     
         if(cooldown < 0)
         {
             //generate a random enemy from the enemy List
             int rand = Random.Range(0,enemyList.Length);
 
             //spawn the enemy
             var enemy = Instantiate(enemyList[rand],transform.position, Quaternion.identity) as Transform;
 
             //add the enemy to the global spawned enemy list in GameScript.cs
             gameController.GetComponent<GameScript>().spawnedEnemies.Add(enemy);
 
             //get the associated cooldown
             cooldown = cooldownList[rand];
         }
         
         cooldown -= Time.deltaTime;
 
     }

Additionally, I have an invisible GameController that is tracking the spawned enemies (List spawnedEnemies).

Which brings me to my issue: I have a weapon in the game that is essentially a seeking missile. I call the following function:

     private void acquireTarget()
     {
         //find the game controller
         var gameController = GameObject.FindWithTag ("GameController");
 
         //if there are untargeted enemies
         if(!gameController.GetComponent<GameScript>().noTargets)
         {
             lockedTarget = gameController.GetComponent<GameScript>().getTarget;
         }
     }

getTarget basically builds a new List of all existing objects who have the bool isTargeted set to false (essentially populating the list with eligible targets) and then returns one at random.

I then call the following two lines of code at the end of my Update function to attempt to move the missile:

         transform.position = Vector3.MoveTowards (transform.position, lockedTarget.transform.position, .03f);
         transform.LookAt (lockedTarget.transform);

What actually happens is that the missile object stops drawing on screen, and I am thrown the following error in the console:

UnassignedReferenceException: The variable lockedTarget of 'MissileBehavior' has not been assigned. You probably need to assign the lockedTarget variable of the MissileBehavior script in the inspector. UnityEngine.Component.get_transform () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineComponent.cs:19) MissileBehavior.Update () (at Assets/Scripts/MissileBehavior.cs:41)

Line 41 is the Vector3.MoveTowards. The problem is, if I inspect the missile object, it shows as having Locked Target assigned as pinwheel_enemy(clone)(Transform). So I'm not sure what's going on.

Comment
Add comment · Show 3
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 robertbu · Dec 26, 2013 at 04:30 AM 1
Share

With this chopped up snapshot of your code, it is hard to figure out where things are going wrong. Usually I appreciate someone isolating the suspect code areas, but in this case, I think we need to see the entire script. If it is larege, you can use pastebin

avatar image KellyThomas · Dec 26, 2013 at 04:38 AM 1
Share

With the following example, why are you using type var?

  var enemy = Instantiate(enemyList[rand],transform.position, Quaternion.identity) as Transform;

What type is lockedTarget? Can we see the code for getTarget?

avatar image Nephxio · Dec 26, 2013 at 06:14 PM 0
Share

Sure can!

I'm using var mostly because I am still working at understanding what types are passed around by Unity. I realize var is really not optimal when you can actually strongly type it ins$$anonymous$$d. So I will admit to it being really bad form (and plan on fixing it once I "get" it better)

lockedTarget is set to be a public Transform in the same script as the mentioned Update function.

Here's the code for getTarget:

     public Transform getTarget
     {
         get
         {
             List<Transform> targets = new List<Transform>();
             foreach(Transform elem in spawnedEnemies)
             {
                 if(!elem.GetComponent<HealthScript>().target)
                 {
                     targets.Add (elem);
                 }
             }
             return targets[Random.Range (0,targets.Count)];
         }
     }


This is my first foray into C# (C++ was my first language) so I'm fumbling a bit at times to do things I normally would do. $$anonymous$$y thought process for lockedTarget was to try give it a reference to a spawned object to get the missile to it, and the OnTriggerEnter2D code would handle the rest for me (it does for other weapon types). I just haven't figured out how to get that reference set I guess.

3 Replies

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

Answer by Nephxio · Dec 28, 2013 at 02:14 AM

Thanks guys, this answered a ton of my questions, so going to chew on this a while and see if I can get it working (and mark this answered)

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
1

Answer by Roland1234 · Dec 26, 2013 at 07:30 PM

From what I can tell based off the code snippets - if you're getting an UnassignedReferenceException but when you inspect the object you actually see an assigned reference, it seems reasonable to me to assume that you are assigning the reference a frame too late. Try replacing the 2 lines in your missile's Update method with this:

 if(lockedTarget != null)
 {
     transform.position = Vector3.MoveTowards (transform.position, lockedTarget.transform.position, .03f);
     transform.LookAt (lockedTarget.transform);
 }
 else
 {
     Debug.LogError("lockedTarget is null!");
 }

And see if you get the error message logged to the console. If so, then you'll have to go into more detail regarding when you call your acquireTarget() method and whatever else might actually be happening. Posting the entire missile MonoBehaviour would help.

Comment
Add comment · Show 2 · 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 Nephxio · Dec 26, 2013 at 09:25 PM 0
Share

I went ahead and put the 3 relevant scripts in pastebin here:

SpawnerScript.cs http://pastebin.com/4y2DdJJu (spawns enemies & adds them to the global array)

GameScript.cs http://pastebin.com/PqvU0Jwa (attached to an invisible object)

$$anonymous$$issileBehavior.cs http://pastebin.com/i6A6HXrp (attached to $$anonymous$$issile Prefab)

It is returning lockedTarget is null in the console as well.

avatar image Roland1234 · Dec 27, 2013 at 08:16 AM 0
Share

The acquireTarget method is checking your GameScript.noTargets property, which is likely returning true and causing no target to be acquired. Looking at the property I see you are returning true if any spawned enemy has a HealthScript.target property that is true, which looks to me like you're saying "There are no targets if any spawned enemy has been targetted," which I'm pretty sure is a mistake.

I think you meant something like this:

 public bool noTargets
 {
     get
     {
         foreach(Transform elem in spawnedEnemies)
         {
             if(!elem.GetComponent<HealthScript>().target)
             {
                 return false;
             }
         }
         return true;
     }
 }

Still, I can't find any reason why you would be seeing Locked Target assigned in the inspector while getting the error, can you verify and consistently reproduce this?

avatar image
0

Answer by KellyThomas · Dec 27, 2013 at 02:41 AM

acquireTarget() is not guaranteed to find a target:

 private void acquireTarget() {
      //find the game controller
      var gameController = GameObject.FindWithTag ("GameController");
      //if there are untargeted enemies
      if(!gameController.GetComponent<GameScript>().noTargets) {
          lockedTarget = gameController.GetComponent<GameScript>().getTarget;
          hasTarget = true;
      }
 }

If noTargets return true then lockedTarget will remain unchanged. You will need any code that follows your call to acquireTarget() to handle this situation gracefully.

On a more cosmetic note here a couple of changes that could be made to tighten up the typing:

 // SpawnerScrpt, line 39 Instantiate has a generic form.
 //var enemy = Instantiate(enemyList[rand],transform.position, Quaternion.identity) as Transform;
 Transform enemy = Instantiate<Transform>(enemyList[rand],transform.position, Quaternion.identity);


 // MissileBehavior, line 41 lockedTarget is already of type Transform
 //transform.position = Vector3.MoveTowards (transform.position, lockedTarget.transform.position, .03f);
 transform.position = Vector3.MoveTowards (transform.position, lockedTarget.position, .03f);
Comment
Add comment · Show 2 · 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 Nephxio · Dec 27, 2013 at 05:32 AM 0
Share

I had considered that with lockedTarget, that it needed a default target (preferrably one I can check in that if statement) but I'm not sure how to do it. The obvious choice to me is GameController, but it appears that objects placed in the scene via Unitye, even if tagged, are not easily converted to Transforms. Am I missing something obvious?

avatar image KellyThomas · Dec 27, 2013 at 06:09 AM 0
Share

Well if you just want it to shoot forward this should do it:

 transform.position += transform.forward * 0.3f;

But it will rely of have an appropriate rotation.

We haven't seen the code to actually fire these missiles. As a worst case scenario you could add something like this to $$anonymous$$issileBehavior's Start() method:

 GameObject player = GameObject.Find("Player");
 if (player) {
     transform.rotation = player.transform.rotation;
 }

However running GameObject.Find() this often is bad practice. You can probably pass an appropriate rotation to your Instantiate() call without having each missile search out the player GameObject.

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

An OS design issue: File types associated with their appropriate programs 1 Answer

How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers

How to Spawn an Object in between Randomly Spawned Objects with no Overlapping? 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