- Home /
Problem with UI Button calling function [4.6 UI]
I've created a toggle list, where each toggle changes the unit to create, like an RTS. However, the function I want to use doesn't show up in the event list for reasons I don't know. Here's the code:
//Unit to deploy
public Soldier UnitToDeploy;
//List of deployable units
[System.Serializable]
public class Soldier {
public GameObject soldier;
public int soldierCost;
}
public Soldier Riflemen, SpecOps, Gunner, Sniper, Support;
//public void to change the UnitToDeploy
public void ChooseUnit(Soldier soldier)
{
UnitToDeploy = soldier;
}
Does anyone have any ideas why I can't even select the ChooseUnit function?
I have a feeling your problem stems from the Soldier parameter. Looks like you can only use functions with primitive types. Perhaps add a helper method in that lets you take an ID (int) and cast that to a Solider object.
public void ChooseUnit(int soldierID)
{
UnitToDeploy = IntToSolider(soldierID);
}
private Soldier IntToSoldier(int id)
{
// big switch statement, dictionary lookup, etc.
}
Answer by Tumtable · Jan 03, 2015 at 04:54 AM
The new UI events can only call public functions with primitive parameters and no return, for exemple: public void ChooseUnit(int soldierID) Take a look at this video here: http://unity3d.com/pt/learn/tutorials/modules/beginner/ui/ui-button
Almost perfect. You can also pass a Unity object. So anything that inherits from scriptable object or monobehaviour can also be passed.
Your answer
