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 /
  • Help Room /
avatar image
1
Question by ComanderXXY · Apr 30, 2015 at 09:45 PM · variableinspectorcomponent

Set a public Component variable in the Inspector

Situation:

I have a variable type of Component and whant to set it in the Inspector.

But as you can see in the screenshot, I can select only GameObjects and their "Transform Components"

So now my question is:

How can I set the public Component variable in the Inspector to other Components?

Screenshot:

I created a public Component variable "testVarTypeOfComponent" and tried to set it to a Component of the "Door Left-GameObject", but I can select only the "Door Left-GameObject" and it's "Transform Component" alt text

More Backgroundinformation (I think not Important)

I made a script to detect some Objects (like a lever) for a Prefab. Now I whant it to set bool variables in several other Scripts. But I want it generally and not set to one of the Scripts so I can't use "getComponent". Else I whant to use this Component variable to find the target-variables.

(sorry for my enghish if it's bad)

I hope I'm to on the completly wrong way.

unity.jpg (172.3 kB)
Comment
Add comment · Show 7
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 rageingnonsense · Apr 30, 2015 at 10:09 PM 0
Share

I don't think I quiet follow what you are trying to accomplish. Do you have any code to post?

That being said, the inspector cannot serialize type Component. But what are you trying to store here? Are you trying to store a script reference?

avatar image ComanderXXY · Apr 30, 2015 at 10:40 PM 0
Share

I whant to link a Script-reference (think thats called Component) that I can use it for example to open a door. for example like this:

 //set a variable fron any other script
 testVarTypeOfComponent.activated = true

$$anonymous$$y plan was it to set it in the Inspector and then it doesnt matter on which object this Component is or from which script it cames. The Script would just find what I want.

Is there a way to send personal messages or speak over Skype/TeamSpeak?

avatar image rageingnonsense · Apr 30, 2015 at 11:41 PM 0
Share

I'm still not understanding what you are trying to accomplish here unfortunately.

If it what I THIN$$anonymous$$ though, then what you really want is inheritance?

 class ClassA {
     protected bool activated;
 
     public virtual void Activate() {
         activated = true;
     }
 }
 
 class ClassB : ClassA {
     public override void Activate() {
         activated = true;
         // do something else
     }
 }
 
 
 
 class $$anonymous$$yScript : $$anonymous$$onobehaviour {
     public ClassA script;
 
     public void Start() {
         if(something) {
             script = new ClassA();
         } else {
             // because ClassB is extended from ClassA, we can
             // assign a ClassB to this type
             script = new ClassB();
         }
     }
 }

Is this in essence what you want? You won't be able to assign this in the inspector, but you CAN accomplish it in code.

avatar image ComanderXXY · May 01, 2015 at 12:24 AM 0
Share

YEAH I'm sooo Happy I found a soution !! thanks a Lot

  • in this Thread is explained how to use 2 Inspectors at the same time.

  • In the first Inspector-window I select the Object with the "Component variable"

  • In the second Inspector-window I select the GameObject with the Component that I whant to store in the Variable (see the red-arrow on the Screenshot).

  • Then I Drag the Component of my choice and Drop it on the Variable


Then with this Code:

  testVarTypeOfComponent.activated = true

I can set the "activated variable" in the other script (see the yellow Arrow ini the Screenshot).

alt text

unity-2.jpg (311.0 kB)
avatar image Soumya · Feb 26, 2016 at 12:59 PM 0
Share

@ComanderXXY Hey I know it's been a while but, how did you activate the component from code? In Unity 5.3 there is no componentName.activate property. Can you show your script? Thanks!

avatar image Le-Pampelmuse Soumya · Feb 26, 2016 at 01:25 PM 2
Share

I've converted your answer to a proper comment. Please ONLY use answers when you want to add a solution to the question.

In any other case add a comment. I makes no sense to answer a question with no "answer" in it. ;)

Regarding your comment: As he explained, the .activate variable is something the OP declared in his own test script. It's not part of $$anonymous$$onoBehaviour.

Watch the scripting tutorials. Or check the documentation:

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.html

The variable you are looking for is $$anonymous$$onoBehaviour.enabled

avatar image ComanderXXY Soumya · Mar 04, 2016 at 10:29 PM 0
Share

".activated" is a public variable in the script you see in this thread. I don't know what you're trying, but maybe you're searching for this: GameObject.GetComponent

hope this helps you

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

One specific script won't appear as a component in the Inspector only in Game Mode?? 0 Answers

Problem with the Inspector and Script 1 Answer

Drop down menu for public variables on a component in the inspector. 0 Answers

Inspector value changed automatically to an old value on runtime 0 Answers

Only plus one to variable in other script 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