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 jorgeguimaraes · Mar 02, 2016 at 04:44 PM · exceptionarray of gameobjectsactivate

Problem activate/ deactivate GameObject and array

Hi guys, I have weird problem.

I'm making a shooter game and pooling some missiles. This is the process:

  • Get the pooled missile;

  • this clone searches for enemies on screen;

  • Sets a random target and pursue it;

  • deactivate the target and missile itself (send them back to their own pools) ;

My problems are:

  • when I shoot a missile again (reactivate the missile) it moves to the "dead" enemy transform instead of search for new enemy. How can I make it search for new enemy?

  • if there are no enemies, I get an Exception "ArgumentOutOfRangeException: Argument is out of range.". It doesn't stop the game, but I'm afraid it can bring some trouble in the future.

Here is my Code:

     void OnEnable()
     {
         SearchEnemyOnScreen();
     }
     //------------------------------------------------
     void FixedUpdate()
     {
         if (isHazardOnScreen && hazardOnTarget != null)
         {
             LaunchMissile(hazardOnTarget.transform.position); // Homing Missile
         }
         else
         {
            MoveForward()

         }
     }
    //------------------------------------------------
    void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Hazard")
         {
             other.gameObject.SetActive(false);
             gameObject.SetActive(false);
             isHazardOnScreen = false;
         }
     }
        //------------------------------------------------
        private void SearchEnemyOnScreen()
         {
 
             objOnScreen = FindObjectsOfType(typeof(GameObject)) as GameObject[];
             List<GameObject> targetsOnScreen = new List<GameObject>();
             foreach (GameObject enemy in objOnScreen)
             {
                 if (enemy.tag == "Hazard")
                 {
                     targetsOnScreen.Add(enemy);
                 }
             }
             if (targetsOnScreen != null)
             {
                 int randomTarget = UnityEngine.Random.Range(0, targetsOnScreen.Count - 1);
                 hazardOnTarget = targetsOnScreen[randomTarget];
                 isHazardOnScreen = true;
             }
         }



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

Answer by Jessespike · Mar 02, 2016 at 05:10 PM

When a list is created, it has 0 items. This is not the same thing as a list being null. You can add another condition in the search function:

 if (targetsOnScreen != null && targetsOnScreen.Count > 0)

It is also important to note that Random.Range's max value for integers is exclusive, that means you don't need to subtract one from the max value.

Random.Range(0, 1) will always return 0.

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 jorgeguimaraes · Mar 02, 2016 at 06:03 PM 0
Share

Hi @Jessespike, thanks. This solved my "ArgumentOutOfRangeException" problem.

Can you also help me with my first problem, my missiles moving to an old spot ins$$anonymous$$d of search a new enemy?

I tried this: I added this lines to my FixedUpdate():

 void FixedUpdate()
      {
          if (isHazardOnScreen && hazardOnTarget != null)
          {
             Launch$$anonymous$$issile(hazardOnTarget.transform.position); // Ho$$anonymous$$g $$anonymous$$issile
             Debug.Log("Hazard on screen found by: " + gameObject.GetInstanceID());
          }
          else
          {
             $$anonymous$$oveForward();
             Debug.Log("No hazards found by: " + gameObject.GetInstanceID());

          }

And for my surprise I got a Log like this:

(!) Hazard on screen: -775606 // first shot, it found my unique enemy on screen and deactivated it.

(!) No hazards: -775606 // Second shot O$$anonymous$$, there were no enemies, so move forward

(!) Hazard on screen: -775624 // Third hot. Ops, a new instance is moving to the deactivated enemy gameobject transform.position

(!) No hazards: -775606 // Fourth shot O$$anonymous$$, as expected

// Then I reactivated my enemy gameobject an changed it position

(!) Hazard on screen: -775606 // Fifth shot O$$anonymous$$, found the new enemy GO and deactivated it again (Perfect)

(!) Hazard on screen: -775624 // Sixth shot. this instance is moving to the new deactivated enemy gameobject transform.position;

(!) Hazard on screen: -775636 // Same behavior as -775624

I don't understand why new clones are "inheriting" the first hazardOnTarget.position, and more, why they aren't doing the same when it is deactivated.

avatar image Jessespike · Mar 02, 2016 at 07:17 PM 0
Share

I'm not sure. I did notice that hazardOnTarget never gets set to null after deactivation, maybe that has something to do with it.

avatar image
0

Answer by jorgeguimaraes · Mar 02, 2016 at 08:25 PM

You're right, I didn't set it to null, but my guess now is:

  • I instantiate 10 clones with my pooling script;

  • All 10 clones get the same enemy as target, because instantiating enables the object, even if the script immediately disables them;

  • my first missile clone hits the target, updates itself and "delete" it, so next time it's called it just moves forward;

  • while my first clone updates its list, the second don't, and my "Search" is called once inside OnEnable(), and unfortunately I don't have an OnActivate();

I will try to create a second script that:

  • search and spot a new target = myTarget;

  • get pooled object obj;

  • obj.MyMissileScript.SearchEnemyOnScreen(myTarget);

Am I on the right direction?

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

48 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

Related Questions

Unity Tests Deactivates Test Object 0 Answers

Faulty NullReferenceException? 2 Answers

Null Reference Exception and not sure where I went wrong? Script and two screenshots attached 0 Answers

Windows Standalone and MySQL: NotSupportedException 0 Answers

Array index out of range c# 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