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 Tofudude624 · Apr 25, 2012 at 01:29 AM · variable

Invoking a function in multiple scripts...

Hi. If I have several instances of an object/script, how can I invoke a function in all of them (for example, each instance of Script A has variable B in it, each variable B has a different value, but I want to subtract 1 from every variable B)? I know that static functions cannot reference instance data (like variables that would have different values ...right?). Is there a way to work around this?

Comment
Add comment · Show 4
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 maroonrs2 · Apr 25, 2012 at 02:45 AM 0
Share

use a for command.

avatar image Tofudude624 · Apr 25, 2012 at 03:09 AM 0
Share

I'm confused. Do you think you could elaborate a bit?

avatar image kolban · Apr 25, 2012 at 03:35 AM 0
Share

Can you clarify ... do you have a Script A attached to multiple objects and what you are asking is for a function in the script to be executed once for each instance of the Script found in each of the objects that has it attached?

avatar image Tofudude624 · Apr 25, 2012 at 04:17 AM 0
Share

yes, exactly.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AchillesWF · Apr 25, 2012 at 04:18 AM

You could have all your objects with Script A be children of a single global game object and then send that global game object a BroadcastMessage indicating a Method in Script A that subtracts 1 from variable B.

http://unity3d.com/support/documentation/ScriptReference/GameObject.BroadcastMessage.html

Of, you could just use GameObject.FindGameObjectWithTag() and have tagged all the objects with Scipt As. Then, loop through the results and SendMessage() to them to call the Method subtracting 1 from variable B.

http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

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 Tofudude624 · Apr 25, 2012 at 07:09 AM 0
Share

Thanks for the reply. $$anonymous$$y code has gotten more complex that just subtracting a value from a variable. I put everything in Script A that I want to invoke in function "Level". When I try to send the message, I get this error "GameObject.FindGameObjectsWithTag("Tiles").Send$$anonymous$$essage("Level");" What am I missing?

avatar image
0

Answer by Fabkins · Apr 25, 2012 at 04:19 AM

What you want to use is : gameObject.sendMessage()

Think it may do what what you need:

http://unity3d.com/support/documentation/ScriptReference/GameObject.SendMessage.html

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Are there restrictions on assigning GameObjects to Prefab script variables? 3 Answers

How do I reassign the result of a function back to the original arguments? 3 Answers

Access to script global var's from a transform object? (specific ? on something that should work) 2 Answers

Increasing a score value? 2 Answers

Save what you write in textarea as variable? 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