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 mrsev · Dec 15, 2016 at 10:04 PM · scripting problemvalueaccessgamestate

Change values in ALL objects possessing the script

Searched for that but no valid answer!

I created a few lights and I want them all to turn OFF when more than two are turned ON.

Every 3D switch toggles one light via the Switch script, and adds 1 to the number of lights ON in a script apart named Gamestate (a fake one, but I guess using one could maybe be an answer).

Here are pics, thank you very much for your time ! alt text

alt text

swiutch.jpg (221.8 kB)
swiutch2.jpg (254.9 kB)
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
1
Best Answer

Answer by JedBeryll · Dec 16, 2016 at 07:45 AM

You should keep a static list or array of all switches and when the counter reaches 3 you turn off all of them. If you don't want to keep a list of switches you can tag them and get all with GameObject.FindGameObjectsWithTag to find them which is relatively fast if you don't do this too often. Alternatively since you already have update functions in the switches, you can just check if counter is above 2 and if it is, turn it off.

Comment
Add comment · Show 5 · 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 mrsev · Dec 16, 2016 at 09:33 AM 0
Share

I will go for the tags, even though I'd really like to know how to use arrays or a static list! Thanks a lot I'll accept the answer later.

avatar image JedBeryll · Dec 16, 2016 at 10:53 AM 0
Share

put these in the Switch class:

 public static List<Switch> switches = new List<Switch>();
 
 void Start() {
      switches.Add(this);
 }

You need the System.Collections.Generic namespace for the list.

avatar image mrsev · Dec 16, 2016 at 03:13 PM 0
Share

I'm almost there, but when calling all the objects, how do I change a value inside them? Or simply disactivate them?

GameObject.FindGameObjectsWithTag("switch").??? there's no enabled or Active() I could use...

avatar image JedBeryll · Dec 16, 2016 at 04:13 PM 0
Share

that method returns an array so you have to iterate it

 foreach(GameObject go in GameObject.FindGameObjectsWithTag("switch")) {
      Switch sw = GameObject.GetComponent<Switch>();
      sw.Deactivate(); //or whatever
 }
avatar image mrsev JedBeryll · Dec 16, 2016 at 06:32 PM 0
Share

Finally opted for this (with the tags it was only deactivating the last one)

     void Shutting()
     {
         foreach (GameObject switchz in switches)
         {
             switchz.GetComponent<Switch>().shutdown = true;
         }
     }

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

101 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

Related Questions

How to access to a variable in shader from a script ? 1 Answer

I cant access a script that I made in Assets/Scripts from the FirstPersonCharacter script. 1 Answer

Can not access an abstract class from another script! 0 Answers

Microsoft Access Database Unity 0 Answers

TMP Dropdown resets its value 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