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 /
avatar image
0
Question by SpectralEdge · Oct 06, 2012 at 04:16 AM · c#getcomponentenabled

Enable script on parent

I am trying to get a script on the parent by a certain name and enable or disable it when there is a collision.

 void OnCollisionEnter(Collision colliion)
     {
         transform.parent.GetComponent<scriptName>().enabled = false;
     }

According to every other post I have read, this should work. But, the option to enable does not exist. What am I doing wrong?

Comment
Add comment · Show 2
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 whydoidoit · Oct 06, 2012 at 08:08 AM 0
Share

scriptName is the name of a class and the script file it is in? It's inherited from $$anonymous$$onoBehaviour?

avatar image SpectralEdge · Oct 06, 2012 at 08:31 AM 0
Share

scriptName is the string name of the script to find.

2 Replies

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

Answer by kmeboe · Oct 08, 2012 at 07:34 PM

GetComponent returns type "Component", which doesn't have an "enabled" member. Try the following code (assuming your script's class name is "MyScript"):

 void OnCollisionEnter(Collision colliion)
     {
        MyScript script = transform.parent.GetComponent("MyScript") as MyScript;
        if (script)
            script.enabled = false;
     }
Comment
Add comment · Show 7 · 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 kmeboe · Oct 08, 2012 at 07:49 PM 1
Share

Also, perhaps more importantly, here is the answer for your other "problem":

http://www.oocities.org/safety4baby/images/ToddlerReins.jpg

:)

avatar image SpectralEdge · Oct 08, 2012 at 07:52 PM 0
Share

Tried the last part of the solution. I think there may be a problem with it. Now ins$$anonymous$$d of randomly smacking my keyboard it is flopping around like a fish while making strange keening noises. Please advise?

avatar image kmeboe · Oct 08, 2012 at 07:56 PM 1
Share

Hmm...I'm out of answers at this point, as I have the same problem myself. Perhaps we have to be satisfied with serving as an object lesson for others.

avatar image whydoidoit · Oct 08, 2012 at 08:00 PM 0
Share

The better way to get a component in C# is:

  transform.parent.GetComponent<$$anonymous$$yScript>()

The string version is significantly slower.

avatar image kmeboe · Oct 08, 2012 at 08:25 PM 1
Share

Right....$$anonymous$$ike's point is that the generic version of the method (<>) is faster -- good to know. The key portion of my above answer is the "As $$anonymous$$yScript" cast, which then allows you to access members of that class (or baseclasses).

Show more comments
avatar image
0

Answer by OperationDogBird · Oct 06, 2012 at 06:56 AM

Im not an expert in c#, but i think it would look like this

 transform.parent.GetComponent<scriptName>().enabled = false;

Just moved the () directly after the script name

Comment
Add comment · Show 1 · 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 SpectralEdge · Oct 06, 2012 at 07:17 AM 0
Share

Sorry, yes it is that way, sorry was typing it out with my toddler fighting for the keyboard. Will edit it.

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

12 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

Related Questions

Multiple Cars not working 1 Answer

GetComponent keeps returning null 3 Answers

gameObject.GetComponent("Script").enabled = true not working 5 Answers

C# - Problem with trigger that won't activate 1 Answer

C# GetComponent, component not turning off. 2 Answers


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