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 /
avatar image
0
Question by Ryujose · Dec 16, 2017 at 04:06 PM · scripting problemscriptingbasicsscriptableobject

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!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Hellium · Dec 16, 2017 at 04:24 PM

 PlayersSpawn playerSpawnScript = _scriptContainer.GetComponent<PlayersSpawn>();
 playerSpawnScript.enabled = true ;
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 Bunny83 · Dec 16, 2017 at 04:57 PM 2
Share

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).

avatar image pako · Dec 16, 2017 at 05:07 PM 2
Share

@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:

https://unity3d.com/learn/tutorials/s/scripting

avatar image
0

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;


Comment
Add comment · 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

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

133 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

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


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