- Home /
The question is answered, right answer was accepted
How to select Object's voids through Inspector tab?
Issue:
I'm having some trouble. I want to be able to select any public void on the Objects I set up in the Inspector, but it's a really tricky thing to figure out, because Unity don't give any tips on how to do it. I imagine something like the Button.OnClick(). I set an Object, and then select a method.
I don't have any piece of code to show.
Conclusion:
As I'm not being able to figure it out, and don't know how to search it on Google, I'm asking here.
So, thanks in advance.
Answer by M-G-Production · Aug 16, 2017 at 05:08 PM
There are lots of ways to do it!
If you want to do it like Button component, Look for UnityEvents. Example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class CertainClass: MonoBehaviour
{
public UnityEvent theEvent;
public void CertainVoid()
{
theEvent.Invoke();
}
}
You could also create an Enum with all the voids stored in it like this:
public enum AllVoids
{
None,
Void1,
Void2,
Void3
}
public AllVoids selectedVoid = AllVoids.None;
public void CertainVoid()
{
switch(selectedVoid)
{
case AllVoids.Void1:
Void1();
break;
case AllVoids.Void2:
Void2();
break;
case AllVoids.Void3:
Void3();
break;
default:
debug.Log("Please Define a Void");
break;
}
}
Answer by Destolos · Aug 16, 2017 at 06:16 AM
I think you need a custom inspector. Perhaps this Tutorial will help you: https://unity3d.com/de/learn/tutorials/topics/interface-essentials/adding-buttons-custom-inspector
Thanks for the answer, but I don't need a custom inspector for CALLING my methods. I need to drag an object to inspector, to another script, and select the script and the method that I want to use.
I need to do it this way to keep my scripts simple and clean.
Again, thanks for the answer.
Sorry, my fault. Is this what you are looking for: http://answers.unity3d.com/questions/1117974/make-method-variable-show-up-in-inspector.html ?
Follow this Question
Related Questions
How to keep a moving object at the height of the terrain? (script) 2 Answers
How to move game object towards mouse on x and z only? 1 Answer
Display the same string in multiple text boxes 1 Answer
How to change azure spatial anchor objects? 1 Answer
Best way to defining scripts within a list inside the editor? 1 Answer