- Home /
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: I can't really work out how to do it, apart from in a script (just an example I put in):
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
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);
}
}
}
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?
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
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.
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();
}
}
Your answer
Follow this Question
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