- Home /
How can I activate a GameObject Component of type script?
Hello community.
I'm trying to access an script as component inside a GameObject.
This is the code:
Component playerSpawnScript = _scriptContainer.GetComponent(typeof(PlayersSpawn));
But when I try to set it to Active, I don't see any property or method that let me do it.
I'm with .NET 4.6 experimental.
Regards!
Answer by Hellium · Dec 16, 2017 at 04:24 PM
PlayersSpawn playerSpawnScript = _scriptContainer.GetComponent<PlayersSpawn>();
playerSpawnScript.enabled = true ;
Hellium showed the preferred and usual way to access script components. However if you want / need to use a System.Type parameter for some reason you can do
void SetScriptEnabled(System.Type aType, bool aState)
{
$$anonymous$$onoBehaviour inst = ($$anonymous$$onoBehaviour)GetComponent(aType);
inst.enabled = aState;
}
Note that the base class Component doesn't have the concept of "being enabled". The Behaviour class (which is derived from Component and the base class for $$anonymous$$onoBehaviour) introduces the enabled property. Therefore every script you write has an enabled property. Note that some built-in components of Unity also have an enabled property but are not derived from Behaviour. So their enabled property is a completely seperate property and can't be treated in an abstract way.
Though "scripts" (aka classes derived from $$anonymous$$onoBehaviour) all have a common enabled property due to their common ancestor (Behaviour).
@Ryujose the Component
class doesn't have an "enabled" property, this is why you can't enable it (strictly speaking we activate/deactivate GameObjects and enable/disable scripts):
https://docs.unity3d.com/ScriptReference/Component.html
An OOP refresher:
Since your PlayersSpawn
script -apparently- derives from $$anonymous$$onoBehaviour
, which derives from Behaviour
, which derives from Component
, you can actually assign the PlayersSpawn
script to a variable of type Component
per your code, i.e. all PlayersSpawn
types are also Component
types.
However, it's not only that a Component
doesn't have an enabled property, but since you declare the playerSpawnScript
to be of Component
type, you won't be able to access any of its members e.g. its variables and methods. In other words, playerSpawnScript
in your code, can only function as a Component
and not as a fully fledged playerSpawnScript
.
The most general (least derived) Unity class which inherits from the Component class, and has an "enabled" property is Behaviour:
https://docs.unity3d.com/ScriptReference/Behaviour.html
However, if you don't just want to enable/disable, but fully functioning playerSpawnScript
you should follow what @Hellium said in his answer.
It would also be very beneficial for you to do some scripting tutorials:
Answer by abhaychandna · Dec 16, 2017 at 04:29 PM
public GameObject gameObjectWhichHasTheScriptAttached;
//write this where you want to disable the script
gameObjectWhichHasTheScriptAttached.GetComponent<nameOfScript>().enabled = false;
Your answer
Follow this Question
Related Questions
How do I make a photography function? 0 Answers
How to share a script between multiple gameobjects but with different values 3 Answers
looking for a way to set an integer to another number based on a condition. 2 Answers
How to make an enemy ragdoll on death? 1 Answer
How to access and change scriptable objects on instantiated prefabs 1 Answer