• 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 rickonlp · Sep 26, 2020 at 10:30 AM · variable

Acces variables from script that is on multiple objects

I have a pedestal that will be active only if object of right color/material will be on it. Those object can change color if player press certain buttons. How do i find out if the obejct is right color? I was thinking about something like making 3 bool variables called red, green, blue in the script that is on those objects and then switching them between true/false depending on what color the object have. But i dont know how to access those variables only on one object. I would be really thankfull if someone could help me.

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 rickonlp · Sep 26, 2020 at 10:43 AM 0
Share

Ps: I am using C#

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by vivaanbradoo1 · Sep 26, 2020 at 11:04 AM

you can basicaly reference your gameobjects in this case the "coloured objects", for example public gameObject coloredObject1;

then be sure to drag and drop it in the inspector! after that you can simply access everything on that script which is public like (i have stored the result in a bool called isGreen, you can even access int, float etc if you want) coloredObject1.GetComponent().YourBool;

then if its a bool you can check

if(isGreen && isBlue && isRed){ whatever you want to do }

(Note you can access other scripts by making more gameobjects like the coloredObject1)

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 rickonlp · Sep 26, 2020 at 11:19 AM 0
Share

That is one way to do it, but there is problem. Lets say that i have two objects that can change color and one pedestal. Any of those two objects can be placed on the pedestal and the pedestal will turn on only if the placed object is right color. If i understand it if i would use the way that you suggest then only one object that i draged there in inspector would work or am i getting something wrong?

avatar image vivaanbradoo1 rickonlp · Sep 27, 2020 at 07:03 AM 0
Share

you can create and drag multiple gameobjects in the script for each object you have.

like

public gameobject GameObject1; public gameobject GameObject2; etc.

this way you can get each individual values from their scripts of each of the gameObjects.

avatar image
1

Answer by rh_galaxy · Sep 26, 2020 at 02:38 PM

Here is an example of 2 alternatives where you keep track of all objects that are currently existing.

 public class ColoredObject : MonoBehaviour
 {
     internal int color;
     internal bool placedOnPedistal = false;
 }
 
 public class AccessingObject : MonoBehaviour
 {
     public ColoredObject coloredObjectBlueprint; //must be set in inspector
     
     List<ColoredObject> coloredObjectsAlt1 = new List<ColoredObject>();
     ColoredObject[] coloredObjectsAlt2 = null;
     
     void AddNewObject()
     {
         //create new object
         ColoredObject newColoredObject = Instantiate(coloredObjectBlueprint);
         newColoredObject.color = 1; //initialize new obj
 
         //update list of objects alternative 1
         coloredObjectsAlt1.Add(newColoredObject);
 
         //update list of objects alternative 2
         coloredObjectsAlt2 = GameObject.FindObjectsOfType<ColoredObject>();
     }
     
     void Update()
     {
         //using alternative 1
         for (int i=0; i<coloredObjectsAlt1.Count; i++)
         {
             if(coloredObjectsAlt1[i].placedOnPedistal && coloredObjectsAlt1[i].color == 1)
             {
                 //do something
             }
         }
 
         //using alternative 2
         if(coloredObjectsAlt2 != null) {
             for (int i=0; i<coloredObjectsAlt2.Length; i++)
             {
                 if(coloredObjectsAlt2[i].placedOnPedistal && coloredObjectsAlt2[i].color == 1)
                 {
                     //do something
                 }
             }
         }
     }
 }
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 rickonlp · Sep 26, 2020 at 03:58 PM 0
Share

Thanks for your help, i didnt used your idea but when i was trying to do it i came up with different aproach that work.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Transfering Variables Between Sections [SOLVED] 2 Answers

Checking if variable is nullable type? 3 Answers

How can I storing the IEnumerator with parameters? 1 Answer

Passing variable between scripts 1 Answer

Use enum value as a index variable of a list 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