- Home /
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! :)
Is your Standard_Bullet
component on the same object as that script trying to get Standard_Bullet
?
Haha i'm and idiot, thanks it worked! :D You should convert this to an 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
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)
Okay Thanks! Yeah i made a little mistake :P I'm actually using Javascript. But thanks for the info :)
Your answer
Follow this Question
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