Search code examples
c#unity-game-enginegame-development

How to make Button event trigger call methods from my instantiated objects?


In my mobile game (2d side scroller), my player character movements (left , right and jump) are manipulated through buttons with event triggers by utilizing its pointer up (). the catch is, I have a scene before the actual game that lets the player select different character objects then it will instantiate on the actual game scene.

I'm aware that a button needs an object reference slot field where it can call the method of movements from my character script but I can only reference one since there are other possible character objects that relies on that button for movements. Is there a way where I can write a script that when I press my movement buttons, those buttons can recognize and call my movement script from my instantiated character gameObjects without referencing character object on the button event trigger reference slot? Thank you

Here is What I have done so far:

 public class TestPlayerMovement : MonoBehaviour
{

[SerializeField] public float movespeed;
Rigidbody2D rb;
float xAxisMove;
bool moveleft;
bool moveright;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();

}


private void Update()
{
 MovePlayerXAxis();
}


public void PointerUpleft() {
    moveleft = false;
}

public void PointerDownleft()
{
    moveleft = true;
}

public void PointerDownRight()
{
    moveright = true;
}

public void PointerUpRight()
{
    moveright = false;
}

//Checks if player will move left or right
void MovePlayerXAxis() {
    if (moveleft)
    {
        xAxisMove = -1;
    }
    else if (moveright)
    {
        xAxisMove = 1;
    }
    else
    {
        xAxisMove = 0;
    }

    rb.velocity = new Vector2(xAxisMove * movespeed, rb.velocity.y);
}

}

This is the outcome I would liek to have: enter image description here

I hope this flow will you understand my post, thank you for taking your time.


Solution

  • One way to solve this is to use a Singleton object, and have it store a reference list or cache.

    Your Singleton Class:

    public class SomeSingletonClass : MonoBehaviour
    {
        private static SomeSingletonClass instance;
    
        // List of game objects you can store to reference it later
        private List<GameObject> refList = new List<GameObject>();
    
        public static SomeSingletonClass Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = FindObjectOfType<SomeSingletonClass>();
    
                    if (instance == null)
                    {
                        GameObject singletonObject = new GameObject("SomeSingletonClass");
                        instance = singletonObject.AddComponent<SomeSingletonClass>();
                    }
                }
    
                return instance;
            }
        }
    
        public void AddToRefList(GameObject obj)
        {
            if (!refList.Contains(obj))
            {
                refList.Add(obj);
            }
        }
    
        public List<GameObject> GetRefList()
        {
            // Ideally for best practices, you wouldn't want anyone to directly access this list.
            return refList;
        }
    }
    
    

    Creation Class

    private void CreateObject()
    {
        GameObject yourNewObject = Instantiate(/*...*/);
        // Add the newly instantiated object to the global list.
        SomeSingletonClass.Instance.AddToRefList(yourNewObject);
    }
    

    Creation Class

    private void OnClickStuff()
    {
        // Now you have a list of gameobjects that you can call methods on.
        List<GameObject> callMethodsOn = SomeSingletonClass.Instance.GetRefList();
    }
    

    Here are a few different links to learn more about singleton. It should be quite straight forward as it is just a static object, without it being static.