How do I use a function’s return value as a parameter in another function?

I am using Unity 2019.4.20f1

Hi, I am currently trying to dynamically set the Game Object value of a button’s OnClick() method. To do this, I am trying to use a delegate event system, which passes along a reference to the clicked-on-GameObject and a couple of functions.

184010-screenshot-303.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Attatched to InspectMenuController GameObject
public class InspectMenuController : MonoBehaviour
{
    private Transform equipTransform;
    private GameObject currentPrefab;

    public GameObject useButton;
    public GameObject replaceButton;

    private void OnEnable()
    {
        GameEvents.OnMessageSent += SetCurrentPrefab;
    }
    private void OnDisable()
    {
        GameEvents.OnMessageSent -= SetCurrentPrefab;
    }
   
    // This method is called after a GameObject has been clicked on
    // I think it should take the clicked on GameObject reference as a parameter
    // I don't think it is set up correctly
    public void SetCurrentPrefab(GameObject myClickedPrefab)
    {
        GetCurrentPrefab(myClickedPrefab);
    }

    // When this function is called by SetCurrentPrefab it should retrun the passed in GameObject parameter as a value
    // I don't think it is set up correctly
    public GameObject GetCurrentPrefab(GameObject myClickedPrefab)
    {
        myClickedPrefab = currentPrefab;
        Debug.Log("Made it to GetCurrentPrefab");
        return currentPrefab;
    }

    // This function should be called with a button OnClick method
    // Ideally, it would use the GetCurrentPrefab return value to dynamically set the GameObject value in the OnClick method
    // I don't think it is set up correctly
    public void CenterPrefab(GameObject currentPrefab)
    {
        currentPrefab = GetCurrentPrefab(currentPrefab);
        if (currentPrefab.TryGetComponent(out EquipmentClass equipment))
        {
            // currentPrefab = myClickedPrefab;
            Debug.Log(equipment.Name);
            Debug.Log("Hi CurrentPrefab");
        }
    }
}

My project is set up in a way that everytime a GameObject is clicked, it triggers a delegate event which passes on a reference to the clicked-on-gameObject to all of the event subscribers. When the delegate event is triggered it calls the function SetCurrentPrefab(GameObject myClickedPrefab). I am using this function mainly to call the next function GetCurrentPrefab(GameObject, myClickedPrefab). Both methods’ arguments should be filled by the currently selected GameObject. However, as I type this I am realizing that I probably don’t have this set up correctly. GetCurrentPrefab() would then return a GameObject as a value, and that GameObject should be the GameObject which was passed along with the delegate event.

Finally, CenterPrefab(GameObject currentPrefab) should pass in GetCurrentPrefab()’s return value as a parameter for its currentPrefab argument. I am using the out parameter to access the selected-GameObject’s components.

As it is currently set up, I am given the error: ArgumentException: Object of type ‘UnityEngine.Object’ cannot be converted to type ‘UnityEngine.GameObject’

When I manually set the OnClick() parameter to the GameObject I am currently testing on I get the error: NullReferenceException: Object reference not set to an instance of an object.
This error is triggered by line 45, and I think it has something to do with how I’m using the out parameter and the fact that I am probably not passing my GameObject correctly between the previous three functions.

I’m not sure if this is useful but, in the dropdown menu, I only have one option, CenterPrefab( GameObject), rather than the usual two, static and dynamic.

I’m not a competent coder, and there is a good chance a lot of this is nonsense as I’m not always sure if I am correctly using terms and what-not. I would be very thankful for any help!

Ah! I just paid attention to what you said in the paras after the code (i was late last night, sorry i didn’t pay more attention then)…so the first solution i suggested is wrong, you can roll it back, keep the variable names solution tho, since i didn’t understand the jist of the problem, the error lies in the fact that you cannot call a function without providing the adequate parameters.

The correct approach would be to use another parameterless function to call the CenterPrefab() function

public void CallCP()
{
  GameObject prefabReturn = GetCurrentPrefab();
  CenterPrefab(prefabReturn);
}

and remove the

currentPrefab = GetCurrentPrefab(currentPrefab);

line from CenterPrefab() altogether.
Also you need to remove the GameObject parameter from the GetCurrentPrefab function, instead just replace it’s contents entirely with

{
  Debug.Log("");
  return(currentPrefab);
}

Also SetCurrentPrefab is kind of both useless since it only calls GetCurrentPrefab with the same parameter and i don’t think it does the job it’s intended to…so to correct it

public void SetCurrentPrefab(GameObject myClickedPrefab)
{
   currentPrefab = myClickedPrefab;
}

and pass the myClickedPrefab parameter through the inspector (seeing that you aren’t using raycasts directly on objects and instead buttons to do the trick)…and i think this should do…Hope this helps!