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 Wayne_Peters · Feb 03, 2017 at 03:57 PM · getcomponentrenderergameobjects

Effecting other game objects remotely

I'm new to scripting and whilst I've muddled through using this forum, google and Youtube so far, I've hit a bit of a brick wall. I'm trying to figure out how to get a script on one game object to change a property on another game object. For example, I have MasterObject with the script attached to it. I then also have OtherObject01, OtherObject02 and OtherObject03. When an integer variable in the script on MasterObject reads 1 (for example), I want it to tell the renderer in OtherObject02 and OtherObject03 to turn off - ie, set it to false but I cannot figure out how to specify those objects.

Initially I added a script to the OtherObjects that listened for the variable value and turned themselves off if the correct condition was met, but if I have dozens or hundreds of these OtherObjects, that means dozens or hundreds of three-line scripts which seems horribly inefficient. Equally, I have encountered the GetComponent command but as yet have been unable to get it to refer to a specific game object.

I did manage to get this to work:

 public var OtherObject01:  GameObject;
 
 function Update() {
     if(Setup.department == 1) {
         OtherObject01.GetComponent.<Renderer>().enabled = true;
     }
         
     else {
         OtherObject01.GetComponent.<Renderer>().enabled = false;
         }


But this sets up public variables and I have to manually assign the game objects to those variables in the inspector. It must be possible to assign those game objects within the script or just name them directly. I just can't figure out how. Any help would be much appreciated.

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
0

Answer by Wayne_Peters · Feb 04, 2017 at 02:36 PM

After much research and help from an old colleague via social media, the answer seems to be to use Tags and GameObject.FindWithTag. I've tested it and it seems to work. It's pretty cack-handed having to set it up as a variable and not being able to address the game object directly but it works and appears to be the least inefficient way to go.

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 Wayne_Peters · Feb 04, 2017 at 07:15 PM 0
Share

It turns out that this won't work as I can only have nine tags and I need hundreds. The only way I can see of this working is to give each object it's own script that listens for variables. That just seems horribly inefficient but as I'm a newbie to scripting, I can't see any other way of doing it.

avatar image Wayne_Peters · Feb 04, 2017 at 07:27 PM 0
Share

Actually GameObject.Find might be a solution.

avatar image Wayne_Peters · Feb 04, 2017 at 09:11 PM 0
Share

Well GameObject.Find works great and I've switched to deactivating objects rather than shutting off their renderer (because you can't do that with Canvas objects apparently!).

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

90 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

Related Questions

Renderer error 0 Answers

Help with script , Change tag depending on material attached 1 Answer

2 same Scripts w/ GetComponent : one works, one doesn't 2 Answers

How would I access a property of another objects child component? 2 Answers

Get Component Script Error 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