Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 AffreuxLex · Dec 27, 2018 at 06:30 PM · interfacesendmessagefunction call

Call function from variable scripts

I really need a hand-holding answer for this. The direction people have pointed me are just not clicking for some reason. Usually I can grasp new concepts but for some reason, I'm just not comprehending what needs to be done. Something to do with interfaces I guess? Static class? I just don't understand, I blame the Christmas booze. I digress, the following works but as I understand it, it is not desirable.

Problem: I have a prefab NPC. I give it an activity master script. I want to drag and drop a new activity script on to the game object without having to modify the master script.

Existing Code: Master:

 public class NPC_Choose_Activity : MonoBehaviour {
 
     Dictionary<string, int> myActivities = new Dictionary<string, int>();
 
     public void AddNewActivity(string activityName, int activityImportance){
        myActivities.Add(activityName, activityImportance);
     }
 
     private void Update() {
         if (!npcMaster.isBusy) {
             ChooseActivity();
         }
     }
 
     private void ChooseActivity() {
         npcMaster.isBusy = true;
         int highestImportance = 0;
         string activityKey = "";
         foreach (KeyValuePair<string, int> aKey in myActivities) {
             if (aKey.Value > highestImportance) {
                 highestImportance = aKey.Value;
                 activityKey = aKey.Key;
             }
         }
         SendMessage("IsThisMe", activityKey);  // This seems bad.  Is it bad? Sure seems like it
     }
 }



Drag and Drop:

 public class NPC_TestActivityOne : MonoBehaviour
 {
     NPC_Choose_Activity npcMaster;
 
     void OnEnable() {
         npcMaster = GetComponent<NPC_Choose_Activity>();
         npcMaster.AddNewActivity(this.GetType().Name, 500);
     }
 
     public void IsThisMe(string myName) {
         if (this.GetType().Name == myName) {
             GoDoTheThing();
         } 
     }
 
     void GoDoTheThing(){
              // Every drag and drop script will have this function, that's why I think interface? I don't understand the implementation though
              // Just do it
     }
 }

Second verse same as the first:

 public class NPC_TestActivityTwo : MonoBehaviour
 {
     NPC_Choose_Activity npcMaster;
 
     void OnEnable() {
         npcMaster = GetComponent<NPC_Choose_Activity>();
         npcMaster.AddNewActivity(this.GetType().Name, 1000);
     }
 
     public void IsThisMe(string myName) {
         if (this.GetType().Name == myName) {
             GoDoTheThing();
         } 
     }
 
     void GoDoTheThing(){
           // And now for something completely different
     }

    void MoreFunctionsThatNoOtherScriptHas(){
          // Wheeee
    }
 }
Comment
Add comment · Show 3
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 Vega4Life · Dec 27, 2018 at 07:09 PM 0
Share

Questions. Those activities are on gameObjects already in your scene? Or are they being instantiated by something later on?

avatar image AffreuxLex Vega4Life · Dec 27, 2018 at 07:40 PM 0
Share

I'm currently building the prefab so all those scripts are being put on to a game object already in my scene. When I'm done, the whole thing will be a prefab so that when new NPCs are "born" they'll instantiate from this game object.

avatar image Vega4Life AffreuxLex · Dec 27, 2018 at 08:35 PM 0
Share

Hmm. Seems really rough when you have 20+ different activities.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SpaceManDan · Dec 28, 2018 at 12:13 AM

First if there is only one npcMaster ever in your game, go to your npcMaster script and do this

 public static NPC_Master Instance;
 private void Awake ()
 {
     Instance = this;
 }

Then, instead of doing

 NPC_Choose_Activity npcMaster;

 private void Start()
 {
     npcMaster = GetComponent<NPC_Choose_Activity>();
 }

like you have been, you can instead access the npcMaster without storing a reference, like this:

 NPC_Master.Instance.anyPublicVariableOrFunction

Keep in mind you still need to define private from public variables in the master for them to be available.

Now, you need to make a standard activity class that uses inheritance.

 public class NPC_TestActivity : MonoBehaviour
 {
     //NOTICE the keyword virtual here...
     public virtual void OnEnable()
     {
         NPC_Master.Instance.AddNewActivity(this.GetType().Name, 500);
     }

     //NOTICE the keyword virtual here...
     public virtual void IsThisMe(string myName)
     {
         if (this.GetType().Name == myName)
         {
             GoDoTheThing();
         }
     }

     //NOTICE the virtual keyword here...
     public virtual void GoDoTheThing()
     {
         IDoThings();
     }
 }

Be of the mind that what you put into NPC_TestActivity is only things you want ALL of the activities to do. But when you have one that needs some customs stuff you'll Inherit from it like so...

 //Take out MonoBehavior and inherit from your own class instead
 public class NPC_TestActivity_SpecialFunction : NPC_TestActivity
 {
     //NOTICE the override keyword here...
     public override void OnEnable()
     {
         base.OnEnable(); //Runs the base code set inside of NPC_TestActivity OnEnable() function.

         //You can just run the base code if you need it to behave the same as all the others or add more like so...  
         string moreStuff = "MAKE MORE STUFF IF YOU LIKE...";
         string soMuchMoreStuff = "MAKE MORE AND MORE STUFF!!! WOOOO!!";
     }

     //NOTICE the override keyword here...
     public override void IsThisMe(string myName)
     {
         //same here, just the base or add more if you like
         base.IsThisMe(myName);
     }

     //NOTICE the override keyword here...
     public override void GoDoTheThing()
     {
         //same here, just the base or add more if you like
         base.GoDoTheThing();
     }

     //NOTICE no override necessary because there is nothing in the base class to override.
     public void MySpecialTasksNOTForOtherNPCActivities()
     {
         DoStuff();
     }
 }

NOWWWWWwwww, you need to go to your npcMaster script and add a public List that you can store these activities.

 public List<NPC_TestActivity> nPC_TestActivities = new List<NPC_TestActivity>();

Now, open the inspector and put the NPC_TestActivity_SpecialFunction script on a gameObject that stores it. For example NPC_GameObject. Now move over to the npcMaster script in the inspector and expand the new list we just created and change the number of elements you want to have in the list to reflect how many activities you want it to hold. Then drag NPC_GameObject(with the specialFunction script on it) into one of the elements you now have available... You just set up inheritance. CLAPPING!!!

Now in script you'll have access to your activities like this

 nPC_TestActivities[0].MySpecialTasksNOTForOtherNPCActivities();

 //or do this too

 nPC_TestActivities[0].GoDoThing();

 //You can access downward, but it doesn't work upward... watch the video linked at the bottom to get that explained I don'wanna right now.

Make sure that zero up there is a 1 or 2 or a 3 or a 4 or what ever number corresponds with the correct activity you set in the inspector. Remember that 0 is the first element, and 1 is the second and so on.

BAM!!!! DONE... Mic drop.

Video on Inheritance WATCH IT DUDE!

Comment
Add comment · 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
0

Answer by toddisarockstar · Dec 28, 2018 at 11:39 AM

you can just add the new script in runtime when you want your activity to happen. your triggered function would be the start function of the activity script. in the master script you just need to store the string name of the activity script for lookup.

 public class masterscript : MonoBehaviour {
     //type the name of your activity script here 
     //in the inspector
     public string nameofscript;
 
 
     public void DoActivity(){
         gameObject.AddComponent (nameofscript);
         // new script should be doing some stuff
     }
 
     }
Comment
Add comment · 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

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

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

Child object collision 2 Answers

Calling a function I don't know the name of? 1 Answer

How to activate a function (or equivalent) in a specific gameobject 1 Answer

Making a blocker like dropdown menu for another gameobject. 0 Answers

Unity Layout turning black when resizing 0 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