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 Sveyh · Mar 30, 2014 at 08:04 PM · script errorclick objectsaccessing from any script

C# disable 2 diffirent scripts OnTriggerEnter

Hello, Im trying to disable 2 diffirentscripts when entering my keypad(its a OnTriggerEnter) and enabling them when clicking on the GUI button Ok after typing in the code to open the door. The scripts I want to disable is the SmoothMouseLook script which I got on my FPScontroller and the other script I want to disable is on my camera which is a MouseLook script (my Camera is a child of my FPS controller). How would I write if I want to access both of them and disable them OnTriggerEnter? I tried putting GetComponent().enabled = false; but Im getting an error, Object reference not set to an instance of an object.

Anyone can help me out here?

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 getyour411 · Mar 30, 2014 at 08:07 PM 0
Share

http://answers.unity3d.com/questions/675988/c-pausing-the-game-during-gui-keypad.html

You can't do GetCoponent(), you have to be specific about the component name

Show the exact code you are using (in case the above is a cut/paste/formatting error).

avatar image Sveyh · Mar 30, 2014 at 08:11 PM 0
Share

http://answers.unity3d.com/questions/675988/c-pausing-the-game-during-gui-keypad.html is one of my questions but I still don't really understand how this getcomponet actually works.

1 Reply

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

Answer by getyour411 · Mar 30, 2014 at 08:17 PM

The two questions are essentially the same; when OnTriggerXX fires you have a something like col : Collider; with "col" you have access to the GameObject and can walk the hierarchy, such as

 col.gameObject.GetComponent<SmoothMouseLook>().enabled = false;
 col.gameObject.transform.GetChild("childNodeName").GetComponent<MouseLook>().enabled = false;

change childNodeName to be the name of the child node in your player hierarchy that the camera is attached to. Adjust any syntax nuances as needed, the above is off the top of my head.

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

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 Sveyh · Mar 30, 2014 at 08:31 PM 0
Share

I'll try it out, thanks

avatar image Sveyh · Mar 30, 2014 at 09:02 PM 0
Share

Im getting this error while using col.gameObject.transform.GetChild("FirstPersonController").GetComponent().enabled = false; 2 errors on that > The best overloaded method match for UnityEngine.Transform.GetChild(int)' has some invalid arguments second error > error CS1503: Argument #1' cannot convert string' expression to type int'

Im a retard so please explain to me like if you would explain to a monkey or something. Im greatfull someone is trying to explain this to me though.

avatar image VioKyma · Mar 30, 2014 at 09:43 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Transform.GetChild.html

You can only use GetChild to get a child by it's index. $$anonymous$$aybe you could try GetComponentsInChildren or GetComponentInChildren.

eg If you only have one $$anonymous$$ouseLook component (which you should) you can use:

 col.gameObject.transform.GetComponentInChildren<$$anonymous$$ouseLook>().enabled = false;

avatar image Sveyh · Mar 30, 2014 at 09:52 PM 0
Share
     col.gameObject.GetComponent<Smooth$$anonymous$$ouseLook>().enabled = false;
     col.gameObject.transform.GetComponentInChildren<$$anonymous$$ouseLook>().enabled = false;


I got those now on a OnTriggerEnter, The Smoothmouselook gets disabled, but I can still move my camera up and down as my $$anonymous$$ouseLook seems not to get disabled, Im not getting any errors at all though..

avatar image getyour411 · Mar 30, 2014 at 10:53 PM 0
Share

I said the above was off the top of my head and adjust any syntax mods as needed; use transform.Find there are literally thousands of examples on UA/Google showing how to use GetComponent.

Show more comments

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

21 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

Related Questions

all my scripts stopped working after i put a enemy spawner script in 0 Answers

How to access Vive controllers in a script attached on a simple game object(a cube)? 0 Answers

Object reference not set to an instance of an object 1 Answer

Property guiTexture has been deprecated . Use GetComponent() 1 Answer

can not access variable of prefab script 0 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