Friday, June 3, 2022
HomeGame Developmentunity - Passing a way as a parameter of one other methodology

unity – Passing a way as a parameter of one other methodology


I am engaged on a recreation challenge in Unity with a degree and click on interface and I am attempting to make a way that strikes the participant in the direction of a goal object and invokes one other methodology relying on what the goal object is.

I am operating into two issues: first, when I attempt to cross the tactic as a parameter, it instantly runs it. For instance, the next code is given to the Unity UI to create a button that’s alleged to make the participant transfer in the direction of a goal after which open a pop up menu. AB_PathAct is the tactic that strikes the participant, the primary parameter is the participant’s vacation spot, and the second parameter is a technique, CoMuOpen, which AB_PathAct is meant to make use of when the participant reaches the vacation spot. CoMuOpen has it is personal parameters. As a substitute, when the next code runs, each elements occur directly: The participant is shipped in the direction of the goal, and the menu instantly opens. In different phrases, the code is not even attempting to cross the CoMuOpen name as a parameter of AB_PathAct, it is simply calling it.

public void ButtonCMDInvokeTarget()
{
    AB_PathAct(
       r_hit.collider.gameObject, 
       CoMuOpen(
           popMenus[1],
           Digital camera.important.WorldToScreenPoint(
               r_hit.collider.gameObject.rework.place
           )
       ),
       tarDist
    );
}

The second downside is that AB_PathAct is not really taking that methodology as a parameter; when AB_PathAct reaches its vacation spot, it fails to invoke the tactic that’s its second parameter. I do know why that is occurring; CoMuOpen returns a string and AB_PathAct takes a string because it’s second parameter. What I need to do is exchange AB_PathAct’s string parameter with a way.

To place it merely, I would like to interchange two strains of AB_PathAct to make it do what I really need, however I do not know what to interchange them with. All the pieces within the following code features accurately besides the issue code, which is surrounded by asterisks beneath (the precise code would not have them, clearly).

void AB_PathAct(GameObject comTar, **string methodToInvoke**, float tarDist)
{
    if (Vector3.Distance(rework.place, comTar.rework.place) > tarDist)
    {
        agent.vacation spot = comTar.rework.place;
        mr_moveGoal.enabled = true;
        moveGoal.rework.place = new Vector3(comTar.rework.place.x, moveGoal.rework.place.y, comTar.rework.place.z);
    }
    else if (Vector3.Distance(rework.place, comTar.rework.place) <= tarDist)
    {
        agent.ResetPath();
        **StartCoroutine(methodToInvoke);**
    }
}

Is it potential to make a way take one other methodology name as a parameter?

EDIT: This is the CoMuOpen code for reference. It is a quite simple operate that works as supposed when invoked straight or by way of UnityUI. Returning a string is not essential and I am going to most likely take away it.

public string CoMuOpen(GameObject tarMen, Vector3 tarPos)     
    {         
    tarMen.rework.place = tarPos;         
    tarMen.SetActive(true);          
    return "Open Context Menu";     
    }

EDIT 2: DMGregory’s reply labored! This is the brand new code:

void AB_PathAct(GameObject pathTar, float tarDist, System.Motion<GameObject, Vector3> methodToInvoke, GameObject actTar, Vector3 actPos)
{
    if (Vector3.Distance(rework.place, pathTar.rework.place) > tarDist)
    {
        agent.vacation spot = pathTar.rework.place;
        mr_moveGoal.enabled = true;
        moveGoal.rework.place = new Vector3(pathTar.rework.place.x, moveGoal.rework.place.y, pathTar.rework.place.z);
    }
    else if (Vector3.Distance(rework.place, pathTar.rework.place) <= tarDist)
    {
        agent.ResetPath();
        methodToInvoke(actTar, actPos);
        print(pathTar.identify + methodToInvoke);
    }
}

PathAct takes 5 parameters: the thing that’s the participant’s vacation spot, the space at which the participant will cease, the Technique that will probably be known as when that vacation spot is reached, after which the 2 parameters that will probably be handed to the tactic: a gameobject, and a vector3 place. This is an instance of what calling it seems like:

AB_PathAct(r_hit.collider.gameObject, tarDist, CoMuOpen, popMenus[1], Digital camera.important.WorldToScreenPoint(r_hit.collider.gameObject.rework.place));

The most important limitation is that the Technique that will get handed to PathAct must have a GameObject and likewise a Vector3 as parameters, however that is not that large of a deal, and I would experiment with overrides if I have to.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments