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
1
Question by Jason87 · Mar 13, 2014 at 08:07 PM · gameobjectvariablegamecontroller

Changing a variable across multiple instances of a GameObject

Hello Everyone. I need a little bit of help defining the best way to go about a problem I have. Here is the situation..

I have many instances of a prefab that have a fall speed variable. I have another "power up" gameobject that, when destroyed, slows the fall speed of all instances in the scene. I am having issues figuring out the best way to go about this since it will be a mobile app and performance is an issue.

Idea 1: using findObjectsWithTag

  • I thought of using findObjectsWithTag to gather all objects with types of both "orb" and "powerup" in the scene, and then iterate through that array changing the fall speed variable of each to half of what it was. The issue I have with this is there are 2 different tags I need to gather, and I dont think I can do something like findObjectsWithTag(orb && powerup)

Idea 2: Use a static or global variable that all objects in the scene access - I could also use a static or global variable in the game manager script that holds the fall speed of all of the objects in a scene, and then have the objects check with the game manager for what their fall speed should be. This would be too resource heavy because it requires all of the objects in the scene to constantly check for the speed they should be falling at.

I guess I am looking for a way to change one variable and it effects the fall speed of every object in the scene for a set amount of time before it returns to normal. I would be very grateful if someone can offer their advice.

Thanks!

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

1 Reply

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

Answer by haim96 · Mar 13, 2014 at 08:33 PM

you could use static variable but i think that using Events can help you with this. this way, the object doesn't need to check the variable all the time but get notified when it changed. more about events here: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/events

for sure i can tell you NOT to use Tags for this. it slow.

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 Jason87 · Mar 14, 2014 at 02:08 PM 1
Share

Wow this is great information! I am going to watch all of those videos in that intermediate section to become more comfortable with these concepts. I have a feeling I am going to have to restructure my design, but it will be worth the effort.

In that video, the man stated that you basically create a method that other methods can subscribe to in order to be called when the event occurs. He also stated that for every subscription, there should also be an unsubscribe. What about scenes with many instances of an object? Does each separate instance have to subscribe and unsubscribe from the event handler? Does this mean that an event handler is handling multiple instances of a Function()? If I unsubscribe a Function() from the event handler, does that effect all other instances of that object?

Thanks for the help. This is really going to strengthen my program$$anonymous$$g skills. Opens up many more possibilities and makes many processes simpler.

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

Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers

Assigning current color to a variable for fade out (C#) 0 Answers

Communication between objects and other scripts, variables and properties 1 Answer

Game Object referencing from another script 0 Answers

Public variable in script different for every game object. 1 Answer


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