- Home /
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 :D
//DrZarqawi
Answer by Owen-Reynolds · Feb 17, 2013 at 04:24 PM
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.
I think i understand what you mean, and think it will work im looking into it right now.
but i must admit i dont understand the use of inheritance and classes. What is a base class, and how do i make it inherit the scripts?
edit: I got it to work using Send$$anonymous$$essage(); thank you very much, i have been working on all kinds of solutions for days but now i can finally move on to the more complex game mechanics :D
It's likely each new NPC script is 50% cut&paste from the old ones. Inheritance is a way to avoid the cut&paste. You'd make "basicNPC" with all the shared parts, then each new script would say it's a type of basicNPC. You'd only have to write the parts where it's different. If you improve/fix the common parts, the other scripts magically get the improvements.
But, it takes a lot of practice -- like switching to a "better" grip in tennis. You almost have to plan some small throw-away projects just to learn it. There are many, many (non-unity) examples.
Answer by tomekkie2 · Feb 17, 2013 at 12:58 PM
Just find the gameobjects with
GameObject.Find("name");
then find all the sripts:
and http://answers.unity3d.com/questions/59044/get-all-scripts-attached-to-gameobject.html
and disable them.
But in this case i still refer to a specific script name. since all the scripts in the different objects has a different name, this still wont work. if i misunderstand your answer then what name are you reffering to in Find("name")?
GameObject.Find("GameObjectName").GetComponent("ScriptName");
but i have found the GameObject Using RayCastHit. but RayCastHit will return different objects which all have different scripts. So i dont know the scripts name. i do know how GetComponent() works and have used it severel times in other scripts, but in all the other cases i know the scripts name because it is always the same script it will interact with. but here i need to interact with different scripts depending on the gameObject im looking at.
Use "for" loop to go through all hits and check name of each one.
Answer by Sillan · Feb 17, 2013 at 03:05 PM
You could try this : function Update() { GameObject.Find("thename").active = false; //you are saying the object is not active }
$$anonymous$$y object is active, it needs to be so it can run other scripts later on without needing to be activated by the user.
i can write for you what my solution looks like right now though it still does'nt work: #pragma strict
var Layer$$anonymous$$ask = 1 << 8;
var scrArray = new Array();
var isTalking = false;
function Update () {
var hit : RaycastHit;
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, hit, 4, Layer$$anonymous$$ask)) {
GetComponent(GUI_script).DebugText = "Ray Hit Success: "+hit.collider.name;
if (isTalking == false) {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)) {
isTalking = true;
scrArray = hit.collider.gameObject.GetComponents($$anonymous$$onoBehaviour);
scrArray[0].enabled = true;
}
}
}
else {
GetComponent(GUI_script).DebugText = "Ray Hit Fail";
}
}
right now i get the error: 'enabled' is not a member of 'Object'.
Your answer
Follow this Question
Related Questions
Disable SCRIPT HELP!!!! 1 Answer
Make Script Disable 3 Answers
enable/disable specific components 3 Answers
Disable a script from another script 2 Answers
switch scripts? 1 Answer