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 /
avatar image
0
Question by User1564 · Dec 21, 2019 at 09:05 PM · variablesloopgameobjects

Variables on Instantiated Object Cannot Be Changed

I have an enemy AI script that I attach to all my enemy prefabs. In the script, I have a function that can be called to stop the AI from moving towards the player. The function simply changes a couple variables on the script instance. Here is the function:

 public void InvisibleOn ()
     {
         this.hasTarget = false;
         this.search = true;
     }

I have a consumable item that, when consumed, runs through all the game objects in the scene with the tag "Enemy". It then adds them to a list and iterates through the list, calling the "InvisibleOn" function on all the instances. Here is that code (from the "Consumables" script):

 GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
 foreach (GameObject enemy in enemies)
                 {
                     enemy.GetComponent<EnemyAI>().InvisibleOn();
                 }

The issue is that these variables that are changed in the "InvisibleOn" function don't change for any of the instances except one specific one. For example, say I have bandit01(clone), skeleton01(clone), and bandit02(clone) instances in the game. Only the variables on the bandit01(clone) will be changed. The other two instances do not have their variables changed.

I have made the foreach loop Debug.Log all the names of the game objects it adds to the list, and it correctly adds all the instances. I have also made the "InvisibleOn" function Debug.Log the name of the game object it is attached to when it is called. I see all the instances calling that function, yet for some reason only the aforementioned instance actually has its variables changed.

Is there something that I'm missing here?

Comment
Add comment · Show 1
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 lgarczyn · Dec 21, 2019 at 09:05 PM 0
Share

Have you tried FindObjectsOfType ins$$anonymous$$d of FindGameObjectsWithTag ?

1 Reply

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

Answer by MichaI · Dec 21, 2019 at 10:32 PM

Try:

 EnemyAI[] enemies = GameObject.FindObjectsOfType<EnemyAI>();
  foreach (EnemyAI enemy in enemies)
                  {
                      enemy.InvisibleOn();
                  }
Comment
Add comment · Show 3 · 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 User1564 · Dec 23, 2019 at 12:49 PM 0
Share

That did it, thanks much! Out of curiosity, what is the difference between "FindObjectsOfType" and finding objects and getting the components?

avatar image MichaI User1564 · Dec 23, 2019 at 02:10 PM 0
Share

To be honest I don't know why it didn't work in your case. I think that you could misspelled the tag or you tagged the wrong game object. This way is much more efficient and its less likely to make some mistake.

avatar image lgarczyn MichaI · Dec 23, 2019 at 11:44 PM 2
Share

Another possibility is that multiple scripts were on the same objects.

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

121 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 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

comparing variables of gameobjects in an array 2 Answers

GameObject[0].SetActive (false); more than one GamObject possible? 2 Answers

cant set object in prefab object, but can in the scene 1 Answer

same script on multiple objects access to different variables from another script 0 Answers

How does WaitForSeconds pass values? 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