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 DrZarqawi · Feb 17, 2013 at 12:50 PM · getcomponentdisableenable

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

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
0
Best Answer

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.

Comment
Add comment · Show 2 · 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 DrZarqawi · Feb 17, 2013 at 05:26 PM 0
Share

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

avatar image Owen-Reynolds · Feb 17, 2013 at 10:26 PM 0
Share

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.

avatar image
0

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.

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 DrZarqawi · Feb 17, 2013 at 01:40 PM 0
Share

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")?

avatar image ShadowAngel · Feb 17, 2013 at 01:42 PM 0
Share

GameObject.Find("GameObjectName").GetComponent("ScriptName");

avatar image DrZarqawi · Feb 17, 2013 at 01:58 PM 0
Share

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.

avatar image ShadowAngel · Feb 17, 2013 at 02:03 PM 0
Share

Use "for" loop to go through all hits and check name of each one.

avatar image
0

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 }

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 DrZarqawi · Feb 17, 2013 at 04:19 PM 0
Share

$$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

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

13 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

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


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