Disable a GameObjects scripts knowing only the GameObjects name

Hi, I am new to Unity and UnityAnswers and only code in JS.

I’m trying to make a RPG style game, where a FPS Character can walk around and speak with NPC’s.
All NPC’s have different things to say, ask questions, give the player stuff or be dependant of actions in the world (i.e. “you need the blue key”).

Since all NPC’s are different they need different scripts but i want to be able to activate them all the same way.
At the moment my FPS controller casts a ray straight ahead, returning the gameObject it hits.
how do i enable the scripts attached to the specific gameObject (rayhit) without knowing the script name?

Thanks in Advance :smiley:


Just find the gameobjects with


then find all the sripts:

and get all scripts attached to gameobject - Questions & Answers - Unity Discussions

and disable them.

You could try this :
function Update()
GameObject.Find(“thename”).active = false; //you are saying the object is not active

Make sure all NPC scripts have a function like “setStatus(newStatus : bool)”, which sets your paused variable and whatever else you need. Then use SendMessage("setStatus",true); on the NPC gameObject.

The common way to be sure all scripts have things in common is to have them all inherit from a base class. That way it’s automatically spelled the same and you get yelled at if you forget to add one. But you should be able to just add them by hand.

Disclaimer: I’ve never used sendMessage, but it’s a popular topic here.