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 uberokeer · Jul 25, 2013 at 10:14 PM · c#getcomponentenablemousewheel

Disabling a script in c# not working for some reason

Hello, what I'm trying to do is disable a script from another script. The main reason i need to do this is because i'm making a weapon controller, and i have one script that controls 3 of them. 1 for standard bullet, 2 for a smoke grenade, and 3 for a EM bomb. Which disables all guns within a specific range (just and idea). Now i need to disable the script because each script controls a specific weapon. I've tried like almost everything and i keep getting the same error: "Object reference not set to an instance of an object". I've tried all these:

 GetComponent(Standard_Bullet).enabled = true;

Doesn't work. I've also tried:

 gameObject.GetComponent(Standard_Bullet).enabled = true;

Once again doesn't work. I've tried:

 public var StandardBull : Standard_Bullet;
 GetComponent(StandardBull).enabled = true;

Still doesn't work. So that's why i'm asking on here. Keep in mind i am using C#. Also the script i tried are within a If statement, for instance:

 public var StandardBull : Standard_Bullet;
 public var CurrentWeapon : int;
 function Update (){
 if (CurrentWeapon == 1){
     GetComponent(StandardBull).enabled = true;
 }
 }

Also CurrentWeapon is controlled by the MouseWheel, and is clamped from 1 to 3.

Thanks In Advance! :)

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 Benproductions1 · Jul 25, 2013 at 10:59 PM 1
Share

Is your Standard_Bullet component on the same object as that script trying to get Standard_Bullet?

avatar image uberokeer · Jul 25, 2013 at 11:05 PM 0
Share

Haha i'm and idiot, thanks it worked! :D You should convert this to an answer! :)

1 Reply

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

Answer by Benproductions1 · Jul 25, 2013 at 11:23 PM

Hello,

GetComponent only "gets the component" on the object that it is called on.
If your script is on a different object, trying to get the component on another object will not yield any results,
aka null, therefore a NullReferenceException.

Hope this helps,
Benproductions1

Comment
Add comment · Show 5 · 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 uberokeer · Jul 25, 2013 at 11:31 PM 0
Share

Thank you!

avatar image Benproductions1 · Jul 25, 2013 at 11:36 PM 0
Share

No problem :)
Happy deving

avatar image clunk47 · Jul 26, 2013 at 12:47 AM 1
Share

If you want to access a script on another object, you have to define that object, then use GetComponent to get any scripts on it. You were using GetComponent incorrectly for C#. Say you're using this script on your player, and you need to find an object named "Enemy". Say the "Enemy" GameObject has a script on it named "EnemyAI". To access this in C#, do the following. This is a very basic example of how to access a script on another object.

 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour
 {
     GameObject Enemy;
     
     void Awake()
     {
         if(GameObject.Find("Enemy"))
             Enemy = GameObject.Find("Enemy"))
         if(Enemy != null) 
             Enemy.GetComponent<EnemyAI>().enabled = true;
     }
 }

If you are accessing a component on the same object your script is attached to, simply use GetComponent like so:

 GetComponent<Standard_Bullet>(),
 
 NOT
 
 GetComponent(Standard_Bullet)
avatar image uberokeer · Jul 26, 2013 at 12:56 AM 0
Share

Okay Thanks! Yeah i made a little mistake :P I'm actually using Javascript. But thanks for the info :)

avatar image uberokeer · Jul 26, 2013 at 01:33 AM 0
Share

Okay, Thanks :D

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Enable script on parent 2 Answers

GetComponent keeps returning null 3 Answers

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

Disable a GameObjects scripts knowing only the GameObjects name 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