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 /
This question was closed May 11, 2015 at 12:35 PM by fafase for the following reason:

The question is about script interaction and has many answers already.

avatar image
0
Question by dukenukem4343 · May 10, 2015 at 10:20 PM · c#javascriptgameobjectprogramming

Accessing scripts in the same game object

I am trying to disable other scripts in the same game object, some are C# others are java. I have tried the code in the documentation

 Charcontdisable = GetComponent.<MouseLook>();

but this does not seem to work, what would be ideal for what I am trying to achieve?

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

3 Replies

  • Sort: 
avatar image
0

Answer by Cherno · May 10, 2015 at 11:09 PM

A syntax error, and make sure that Charcontdisable is of Type MouseLook.

 MouseLook Charcontdisable = GetComponent<MouseLook>().enabled = false;
Comment
Add comment · Show 4 · 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 dukenukem4343 · May 11, 2015 at 03:52 AM 0
Share

So will this work with both java and C#, and to enable I just switch the boolean from false to true?

avatar image fafase · May 11, 2015 at 10:01 AM 0
Share

This is for C#.

Check the doc for UnityScript version of GetComponent

avatar image dukenukem4343 · May 11, 2015 at 01:10 PM 0
Share

I get these errors using that code

 Assets/scr_P_Health.cs(39,27): error CS0029: Cannot implicitly convert type `bool' to `$$anonymous$$ouseLook'
 
 Assets/scr_P_Health.cs(17,45): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
avatar image fafase · May 11, 2015 at 01:29 PM 0
Share

Don't get me wrong, but you need to learn the basic of program$$anonymous$$g before using Unity. Even for a basic project. Unity is not $$anonymous$$odu, it requires program$$anonymous$$g knowledge. I would suggest to spend some time practicing C# and program$$anonymous$$g tutorial.

avatar image
0

Answer by zckhyt · May 11, 2015 at 09:18 AM

if you're trying to disable "MouseLook," simply

 GetComponent<MouseLook>().enabled = false;

As far as I know, you can't assign the component of an object to a value. Also, there is a typo in your code where you include a 'dot' after GetComponent; the proper syntax is GetComponent<"Component">();

enabled is a public member of all components that toggles whether or not the script is used (roughly speaking). So from any component on the same object you just need to use GetComponent<"Component">().enabled = true/false; to either enable it or disable it.

edit: The angulary brackets were removed by the system because it uses them for text-formatting. This edit puts all the uses into a special field to prevent that.

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 dukenukem4343 · May 11, 2015 at 01:06 PM 0
Share

I get these errors when I attempt to use this

 Assets/scr_P_Health.cs(18,17): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.GetComponent(System.Type)'
 
 Assets/scr_P_Health.cs(40,25): error CS1502: The best overloaded method match for `UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments
 
 
     Assets/scr_P_Health.cs(40,25): error CS1503: Argument `#1' cannot convert `bool' expression to type `System.Type'
 
avatar image zckhyt · May 21, 2015 at 02:03 AM 0
Share

I don't think you used it correctly. $$anonymous$$ake sure you have GetComponent called from the same object as the object with the component that you are trying to enable/disable. Then, within the <>, supply the component's name (in this case it should be $$anonymous$$ouseLook). This is followed by the two parentheses (which should be empty). Now you can access any public member function or variable of the component.

e.g.,

 GetComponent<$$anonymous$$ouseLook>().enabled = false;

source: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

edit: for some reason it's removing the <> from the code I'm trying to provide, which explains what happened to them in my original comment. I'll try putting them into a special field.

avatar image
0

Answer by Code_Nashor · May 11, 2015 at 12:29 PM

You can't access out-of-the-box C#-Scripts from JS-Scripts or converse, they have a different compile time at project start.

Look: http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Making changes to a terrian in game 1 Answer

Transforms are SUPER HARD! 2 Answers

How to make method that gets multiple types? 1 Answer

Destroy object with more clicks depending on Scale? 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