Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cannot Select Script Function in OnClick for Buttons

Discussion in 'UGUI & TextMesh Pro' started by Richardthegreat, Mar 16, 2015.

  1. Richardthegreat

    Richardthegreat

    Joined:
    Mar 9, 2015
    Posts:
    3
    I Can't seem to make a button display any functions from scripts,
    the functions are all Public and void, i dont understand why it wont show the functions in the Onclick Menu

    sing System.Collections;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;


    namespace GameRPS
    {
    public class Calculate : MonoBehaviour
    {
    static void Main(string[] args)
    {
    CalculateHP(1, 0);
    }

    static double Equation(double Damage, double Temp)
    {
    if (Temp > 0)
    {
    double a, b, c;
    a = Damage / 2;
    b = a + Temp;
    c = b - 50;
    return c;
    }
    else
    {
    double d, e, f;
    d = Damage / 2;
    e = d - Temp;
    f = e - 50;
    return f;
    }
    }



    public static void CalculateHP(int userinput1, int userinput2)
    {
    double[,] numbers = new double[,] {
    {75, 25},
    {40, -40},
    {45, 38},
    {60, 35},
    {40, -45},
    {77, 25},
    {8, -60},
    {73, 27},
    {85, 21},
    {32, -58.5},
    {100, 16},
    {34, 50},
    {82, 25},
    {30, -53.5},
    {40, -47},
    {33, 54},
    {27, 59.5},
    {45, 50.5},
    {11, 65.5},
    {36, 52.5},
    {34, 58.5},
    {53, -49},
    {5, -73},
    {17, -68},
    {47, 56},
    {22, -70},
    {100, -32.5},
    {2, -81.5},
    {97, 38},
    {72, 47},
    {102, 32.5},
    {110, 30},
    {70, -45},
    {65, 52.5},
    {100, -30.5},
    {45, -63},
    {92, -52.5},
    {86, 43},
    {100, -38.5},
    {87, -43.5},
    {106, 41.5},
    {86, -50},
    {76, 54.5},
    {96, -46.5},
    {94, 44.5},
    {86, 55.5},
    {96, 48.5},
    {15, 90},
    {10, -91.5},
    {50, 74},
    {66, -87},
    {100, 65},
    {130, 95},
    {98, 58},
    {100, 76},

    };

    //previous intergers already declaired
    //numbers will be imported from array
    double health1 = 75;
    double health2 = 75;
    double endhealth1;
    double endhealth2;
    double statsplayer1;
    double statsplayer2;
    statsplayer1 = Calculate.Equation(numbers[userinput1, 0], numbers[userinput1, 1]);
    statsplayer2 = Calculate.Equation(numbers[userinput2, 0], numbers[userinput2, 1]);
    if (statsplayer1 > statsplayer2)
    {
    double h;
    h = numbers[userinput1, 0];
    endhealth1 = health2 - h;
    if (endhealth1 == 0)
    {
    //player 1 dies
    //add in scene switch here
    Console.WriteLine("player one dies");

    }
    }
    else
    //where stats player 2 > stats player 1
    {
    double i;
    i = numbers[userinput2, 0];
    endhealth2 = health1 - i;
    if (endhealth2 == 0)
    {
    //player 2 dies
    //add in scene switch here
    Console.WriteLine("player two dies");

    }
    }
    }

    public void Attacks(int attack1, int attack2, int attack3, int attack4, int attack5){
    PlayerPrefs.SetInt("Selection1", attack1);
    PlayerPrefs.SetInt ("Selection2", attack2);
    PlayerPrefs.SetInt ("Selection3", attack3);
    PlayerPrefs.SetInt ("Selection4", attack4);
    PlayerPrefs.SetInt ("Selection5", attack5);
    }
    }
    }
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    OK:
    Issue 1: You are using the "static" flag on your public functions. for them to show up for events you can't use this.
    Issue 2: The "Attacks" function has 5 parameters, the maximum Unity UI events can handle at the moment is 4 (but you could extend this yourself :D)

    Hope this helps

    For functions in a script to be accessible by the UnityEvent property drawer on UI components, they must:
    1: be attached to a GameObject
    2: be simple public functions
    3: User base C# data types (int, float, string, etc)
    4: have 0 - 4 parameters
     
    dansonkang likes this.
  3. Richardthegreat

    Richardthegreat

    Joined:
    Mar 9, 2015
    Posts:
    3
    SimonDarksideJ likes this.
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Glad it helped, now soldier on with your UI :D