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

Question Multiple Variable Types In Single Function

Discussion in 'Scripting' started by DotArt, Oct 19, 2022.

  1. DotArt

    DotArt

    Joined:
    Jun 12, 2021
    Posts:
    81
    Hey so im trying to have two variables in one function, i have done it like this but when i try to do the function in a button on click it doesnt come up as an option

    Code (CSharp):
    1.   public void Select(int chrtNum, Image Icon)
    2.     {
    3.         foreach(CharacterSO chrt in characters)
    4.         {
    5.             chrt.RoomActive = false;
    6.         }
    7.         characters[chrtNum].RoomActive = true;
    8.  
    9.         foreach(Image Place in Places)
    10.         {
    11.             Place.sprite = Icon.sprite;
    12.         }
    13.     }
    weirdly i get no errors but think there must be something wrong
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,876
    The on click UnityEvent does not support more than 1 argument.
    You could make 2 methods and link them both to the event instead.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    You can pass in a UnityEngine.Object however, which can basically be anything, like a ScriptableObject instance for example.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ThingDoer : MonoBehaviour
    4. {
    5.     public void DoThisThing ( Object o)
    6.     {
    7.         Debug.Log( "You passed in a " + o.GetType() + " named " + o.name);
    8.     }
    9. }
    And then set the button up as:

    Screen Shot 2022-10-19 at 8.11.50 PM.png

    And when clicked you should see:

    Screen Shot 2022-10-19 at 8.11.45 PM.png

    EDIT: obviously a properly-typed argument is superior to a generic Object type... such as you can define a ScriptableObject that is a "ButtonIntent" and put arguments in that, and that can be far superior to an enum for passing arguments around from buttons.
     
    Last edited: Oct 20, 2022