Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Objective progression

Discussion in 'Scripting' started by DustyShinigami, Aug 9, 2018.

  1. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    I'm trying to figure out how to add objectives to my game and when to trigger the next one when the first has been completed. At the moment I want to see if I can figure it out for myself, but I may eventually have to get additional help soon. For the time being, how would everyone recommend I handle them? I was thinking of doing them with a CoRoutine, but maybe an 'if' statement/bool would be better...?

    Also, though I have a Game Manager, should I add an Empty Game Object to attach the script to to control the different objectives?

    Thanks
     
    Last edited: Aug 9, 2018
  2. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    Maybe you could get some inspiration from an achievement system. Achievements are basically objectives with with a certain progression and conditions applied to them, right? :rolleyes:
     
  3. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    So close and yet... I can't seem to get my objective complete text to pop up. Everything else seems to work so far.

    Code (CSharp):
    1. public class ObjectiveOne : MonoBehaviour
    2. {
    3.     public Text objective;
    4.     public Text checkpointUnlock;
    5.  
    6.     private int required;
    7.  
    8.     void Start()
    9.     {
    10.         required = 0;
    11.         Objective();
    12.         checkpointUnlock.text = "";
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.     {
    17.         if (other.gameObject.CompareTag("Gold"))
    18.         {
    19.             required = required + 1;
    20.             Objective();
    21.         }
    22.     }
    23.  
    24.     void Objective()
    25.     {
    26.         objective.text = "Collect 5 Gold Bars";
    27.         Invoke("Hide", 3f);
    28.  
    29.         if (required >= 5)
    30.         {
    31.             checkpointUnlock.text = "Checkpoint Unlocked";
    32.             Invoke("Hide", 3f);
    33.         }
    34.     }
    35.  
    36.     void Hide()
    37.     {
    38.         objective.enabled = false;
    39.         checkpointUnlock.enabled = false;
    40.     }
    41. }
     
  4. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    In your Hide() method you are disabled certain objects to hide them, I get that, but are you enabling these back again somewhere? Also, you consider using this:

    Code (CSharp):
    1. void Objective()
    2. {
    3.     objective.text = "Collect 5 Gold Bars";
    4.  
    5.     if (required >= 5)
    6.     {
    7.         checkpointUnlock.text = "Checkpoint Unlocked";
    8.     }
    9.    
    10.     Invoke("Hide", 3f);
    11. }
     
  5. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    No. Well, the Checkpoint Unlocked will be after each objective. But the Collect 5 Gold Bars doesn't need to be. I tried what you suggested, but it didn't work. It's weird because I'm trying to incorporate the same method as the Roll a Ball tutorial with the text. I don't get why the second text doesn't work though. Unless I need to link/reference my script for the gold bars...?
     
  6. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    343
    check out the event and delegate system. This video goes over how to implement this for an achievement system and much more.
     
  7. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    If you are ever only disabling the text objects, they are never going to be visible again until you enable them again; is what I'm trying to say. My suggestion was simply a slight, simple and tiny optimization, not bound to your problem in any way.
     
  8. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    Enabling them before any methods begin doesn't work either. I guess I'll have to try linking my game manager script with it.
     
  9. sunseeker1988

    sunseeker1988

    Joined:
    Jul 10, 2014
    Posts:
    343
    This is why im learning about events and delegates\callbacks. I ended up tightly coupling my code. first project, 4 years coding learning all the way. Almost done with my game, but damn will I do stuff different on the next one.
     
  10. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    I'll have to work through that video you've linked :)
     
  11. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    Still having no luck with this. :( I've tried experimenting with this and still can't get it to work. To me, the code makes sense and it should work. Nothing comes up in the console either.

    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     public int currentGold;
    4.     public Text goldText;
    5.     public Text objective;
    6.     public Text checkpointUnlock;
    7.  
    8.     void Start()
    9.     {
    10.         currentGold = 0;
    11.         SetCountText();
    12.         checkpointUnlock.text = "";
    13.         checkpointUnlock.enabled = true;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         objective.text = "Collect 5 Gold Bars";
    19.         Invoke("Hide", 3f);
    20.     }
    21.  
    22.     public void AddGold(int goldtoAdd)
    23.     {
    24.         currentGold += goldtoAdd;
    25.         goldText.text = "Gold: " + currentGold;
    26.     }
    27.  
    28.     void OnTriggerEnter(Collider other)
    29.     {
    30.         if (other.gameObject.CompareTag("Gold"))
    31.         {
    32.             SetCountText();
    33.         }
    34.     }
    35.  
    36.     void SetCountText()
    37.     {
    38.         if (currentGold == 5)
    39.         {
    40.             Debug.Log ("checkpoint unlocked");
    41.             checkpointUnlock.text = "Checkpoint Unlocked";
    42.         }
    43.         Invoke("Hide", 3f);
    44.     }
    45.  
    46.     void Hide()
    47.     {
    48.         objective.enabled = false;
    49.         checkpointUnlock.enabled = false;
    50.     }
    51.  
    52. }
     
  12. DustyShinigami

    DustyShinigami

    Joined:
    Jan 5, 2018
    Posts:
    84
    Okay, I managed to get it working at last. :D However, I want to add my different objectives as separate game objectives under a parent object. I'd rather keep my gold pickup code in my game manager and the code related to the objective in an objective script. I'm having difficulty referencing code from one script to another. I take it this is done using static variables? I can't figure out how to do it though. I've tried following some tutorials, but I can't quite grasp it. :-\