Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by $$anonymous$$ · Sep 23, 2016 at 02:39 AM · scripting problemvariableinspector

Call functions via variable, like a button

Hi there, what I want to seems pretty simple, I want to call a function, but be able to change that easily, such as having it like a button: alt text I can't really work out how to do it, apart from in a script (just an example I put in):alt text

Okay, I'll clear things up a little. At the moment my script goes:

     if(targets[i].hit)
                     {
                         print("All hit");
                         //In here I'd call a function, such as example.DoSomething();
                         //But I don't want to hard code this function in ^^
                         //And do it like a button, where you can change the function in 
                         // The inspector, without it being hard coded
                     }

If you can understand what I mean that'd be awesome if you could reply, thanks

screenshot-14.png (5.2 kB)
screenshot-13.png (2.3 kB)
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
1
Best Answer

Answer by Jessespike · Sep 25, 2016 at 09:05 AM

A simple way is to use SendMessage.

 public GameObject targetGo;
 public string functionName;

 public void DoSendMessage()
 {
     targetGo.SendMessage(functionName);
 }

If you want to call multiple functions then the two variables can be in a list.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class SendMessageInfo
 {
     public GameObject targetGo;
     public string functionName;
 }
 
 public class MessageSender : MonoBehaviour {
 
     public List<SendMessageInfo> sendMessageInfos;
 
     public void OnClick() {
         foreach(SendMessageInfo smi in sendMessageInfos)
         {
             smi.targetGo.SendMessage(smi.functionName);
         }
     }
 }
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 $$anonymous$$ · Sep 25, 2016 at 09:15 AM 0
Share

Thanks! Cleared things up a bit. So is there actually a way to do like I mentioned without typing in strings? Or do I have to do it this way?

avatar image Jessespike $$anonymous$$ · Sep 26, 2016 at 08:42 PM 0
Share

It can be done. I don't know specifically how, but a starting point would be with Editor scripts.

https://docs.unity3d.com/$$anonymous$$anual/ExtendingTheEditor.html

https://unity3d.com/learn/tutorials/topics/scripting/editor-scripting-intro

avatar image
0

Answer by beefyt123 · Sep 23, 2016 at 03:08 AM

First make sure you function is public

 public void DoSomethingFunction()
 {
 //Stuff here
 }

Then attach it to a GameObject, I usually use GameController

Then click the "+" symbol on the OnClick() component (first image).

Attach the GameObject with the script attached into the component.

Then from the drop down list you will be able to select the function you want to be called when the button is pressed.

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 bittarello · Sep 23, 2016 at 11:39 AM

You can do something like that

 class ButtonBehaviour : MonoBehaviour
     {
         public delegate void CustomFunctionEvent();
         public event CustomFunctionEvent OnCustonFunctionEvent;
 
         void Awake()
         {
             this.gameObject.GetComponent<Button>().onClick.AddListener(OnButtonClicked);
         }
 
         void LoadCustomFunction(int id)
         {
             //OnCustonFunctionEvent = null;
             if (id == 1)
                 OnCustonFunctionEvent += ButtonAction1;
             else
                 OnCustonFunctionEvent += ButtonAction2;
         }
 
         void ButtonAction1()
         {
             //...do something;
         }
 
         void ButtonAction2()
         {
             //...do something;
         }
 
         void OnButtonClicked()
         {
             if (OnCustonFunctionEvent != null)
                 OnCustonFunctionEvent();
         }
     }

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

69 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

Related Questions

How to make change variables 1 Answer

How to draw the default read-only object label in a CustomEditor? 2 Answers

How do I assign script as a variable in the inspector? 1 Answer

Player's position in inspector isn't real position 2 Answers

3D game kit Ellen / Show Player Input script variables on the inspector, 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