Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by FarmerJoe · Mar 17, 2016 at 10:27 PM · c#getcomponentmenu

Cannot disable a script from another script?

We are trying to disable the visibility of the menu on a separate script from a different script. We wrote:

 public JoshsMenu menuState;
     void Start () {
         
         menuState = GetComponent<JoshsMenu> ();
 
         menuState.enabled = false;
     }

But we are still getting error: NullReferenceException: Object reference not set to an instance of an object. It stems from the line: menuState.enabled = false; Thank you for tolerating our naiveté.

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

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

Answer by Rajeesh_AR · Mar 18, 2016 at 04:41 AM

@FarmerJoe

Here your GetComponent is not valid.

This script JoshMenu is attached to a GameObject... Right ?? And you need to disable the script of that Gameobject.. (assume that the joshMenu attached gameObject is different)

for that you could use,

     gameObject.GetComponet< JoshMenu >().enabled = false;
 

If the JoshMenu script and the current script are attached to the same gameObject, then you may use:

 GetComponet< JoshMenu > ().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 LazyElephant · Mar 18, 2016 at 06:00 AM 0
Share

This answer is inaccurate. Using just GetComponent<Joshs$$anonymous$$enu>(); will work providing that both scripts are attached to the game object. I just checked it myself in Unity to make sure I wasn't going crazy.

avatar image Rajeesh_AR LazyElephant · Mar 18, 2016 at 07:53 AM 1
Share

I mean, If the scripts are attached to different gameobject

eg:

Public Class script2{

public GameObject go1;

void funct()

{ go1.GetComponent < Josh$$anonymous$$eny> ().enabled = true

}

}

avatar image LazyElephant Rajeesh_AR · Mar 18, 2016 at 09:00 AM 0
Share

yes, if it's on a different game object, you would have to reference that object, but neither of the examples in the answer refer to a separate game object. They would both only work if the scripts were on the same object. Perhaps you could update your answer to clarify, if that is what you mean.

avatar image FarmerJoe · Mar 28, 2016 at 07:12 PM 0
Share

Thank you for your contribution. We did have it attached to the object, but it was on a different object. After fiddling with assigning variables, it all worked with this:

 GameObject.Find("cursor").GetComponent<Joshs$$anonymous$$enu> ().enabled = false;

"cursor" was the object that had the other script "Joshs$$anonymous$$enu" on it.

avatar image
1

Answer by kami1339 · Mar 28, 2019 at 12:33 PM

use this

 public GameObject otherobj;//your other object
     public string scr;// your secound script name
     void Start () {
     (otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
     }



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 WinteryJungle9 · Nov 21, 2019 at 01:50 AM 0
Share

Thanks so much, I've been looking for something like this for a long time.

avatar image
0

Answer by jebemti · Mar 17, 2016 at 11:06 PM

To be able to use GetComponent the script would have to be on the same object that you are calling from.

If you assign the variable in the editor before entering Play mode just remove the line where you try to get it.

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

8 People are following this question.

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

Related Questions

Writing dropdown selections to file 1 Answer

Value doesn't change in game when updated. 2 Answers

Upcast vs GetComponent 1 Answer

Activating Scripts in another Script? 2 Answers

Accessing the sibling of an object 3 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