0
\$\begingroup\$

How do I make a Goal system in unity that must be completed before the level can be passed.

Goals may be:pick up block Get red flag Go to certain position

Here is sample code -

class Goal : Monobehaviour
{
 public event System.Action completed;
 protected void GoalCompleted()
 {
    if(completed!=null)
       completed();
 }
}
 
class CollectCoins : Goal
{
   ...
}
...

But I don't know where to put the script or what to do with it ?

\$\endgroup\$
0
\$\begingroup\$

It's not unusual in Unity to have an invisible "Manager" game object in the scene which holds any such scripts which can not be considered a behavior of any particular gameObject appearing in the game. This allows such manager-scripts to use Unity events like Update.

But when your manager doesn't need any Unity events and you don't need the luxury of being able to see and change its values in the Unity inspector, then you can also implement it as a plain old C# class not inheriting from MonoBehaviour. But because Unity does not have a "main" method as a central entry point into the application (at least not one you can see), you need to instantiate these classes from other MonoBehaviours and store their instances somewhere (within MonoBehaviours or in static variables).

\$\endgroup\$
9
  • \$\begingroup\$ how do I check for completion then \$\endgroup\$ – elliot727 Mar 3 at 9:57
  • \$\begingroup\$ @elliot727 That wasn't your question. Your question was where to put that script. But there are several approaches to that. You could check the state of the game in the Update method to see if all victory conditions are fulfilled. Or you could have other scripts communicate with your manager and tell it that a certain victory condition was fulfilled, which then causes the manager to check if all other victory conditions were also reported as fulfilled. \$\endgroup\$ – Philipp Mar 3 at 10:02
  • \$\begingroup\$ so I make a game manager object and put it on there AND do I put my level loader script on it as well or not \$\endgroup\$ – elliot727 Mar 3 at 10:07
  • \$\begingroup\$ @elliot727 I have no idea what your level loader script does or how it works, so I can not answer that question. \$\endgroup\$ – Philipp Mar 3 at 10:11
  • \$\begingroup\$ it loads the next level when I hit a collider tagged end \$\endgroup\$ – elliot727 Mar 3 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.