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"
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.
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?
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?
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.
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).
@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!
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
".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