Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KnightRiderGuy · Jun 30, 2017 at 10:31 PM · c#booleanif-statementsboolbooleans

Scavenger Hunt List Bools Question

hey hey hey. I have an interesting problem I'm kinda struggling with a little: I'm working on a scavenger hunt scenario where the user can say something like: "We are looking for a rubber spider"

What happens is a bool called: lookingForRubberSpider = false Gets changed to true

Simple enough for that. On the flip side if you say: "Looks like we have found the rubber spider"

Then the lookingForRubberSpider bool gets changed back to false.

This is ideal and it works great.... but what I'm wondering is is there an alternate way for a scenario like this:

Lets say you have found the rubber spider and for some reason you were to say: "We are looking for a rubber spider" To somehow treat the situation as Well we found it once, why are we looking for it again??

Not sure if I made sense there?

My first initial thought was to perhaps set up a secondary bool called: SpiderFound

And perhaps have that bool referenced in order to be able to return with something like: "we already found a rubber spider, why are you looking for it again"

But if I do that I'm wondering how best to set the SpiderFound bool back to false....???

Maybe when the scavenger hunt is over perhaps with a statement like:

"That was a fun scavenger Hunt" SpiderFound = false

Any thoughts on ways to approach something like this?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Jul 01, 2017 at 03:15 PM 0
Share

O$$anonymous$$ After some experimentation I got this to work:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ScavHunt : $$anonymous$$onoBehaviour {
 
     public Text LookingForText;
     public Text FoundText;
 
 
 
     private enum RubberSpiderQuest
     {
         not_started,
         started,
         complete
     }
     private RubberSpiderQuest myRubberSpiderQuest;
 
     void Start (){
         LookingForText.GetComponent<Text>().enabled = false;
 
         FoundText.GetComponent<Text>().enabled = false;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (myRubberSpiderQuest == RubberSpiderQuest.started)
         {
             LookingForText.GetComponent<Text>().enabled = true;
 
             FoundText.GetComponent<Text>().enabled = false;
         }
 
         if (myRubberSpiderQuest == RubberSpiderQuest.complete)
         {
             LookingForText.GetComponent<Text>().enabled = false;
 
             FoundText.GetComponent<Text>().enabled = true;
         }
         
     }
 
     public void lookingForRubSpider()
     {
         myRubberSpiderQuest = RubberSpiderQuest.started;
     }
 
     public void foundRubSpider()
     {
         myRubberSpiderQuest = RubberSpiderQuest.complete;
     }
 }
 
avatar image Jwizard93 · Jul 06, 2017 at 12:23 AM 0
Share

Why don't you just an integer called numberOfSpidersFound. You can increment it how many times you want. You can test if it's greater than 0.

avatar image RobAnthem · Jul 06, 2017 at 07:28 PM 0
Share

This is bad program$$anonymous$$g to specify things like that you are searching for a spider. I would suggest something along these lines.

first your enum, I usually just create an Enums.cs class and remove the class section of it so it only contains enums.

 public enum QuestState
 {
     NotStarted,
     Started.
     Acquired,
     Completed
 }

Then your scavenger hunt items

 [Serializable]
 public class ScavengerHuntItem
 {
     public int id;
     public QuestState state;
 }

then your monobehaviour for the in-game items

 public class Objective : $$anonymous$$onoBehaviour
 {
     public ScavengerHuntItem item;
     public void CollectItem()
     {
         if (Hunt.Instance.itemsList.Contains[item.id))
         {
             if (Hunt.Instance.itemsList[item.id].state != QuestState.Acquired)
                 Hunt.Instance.itemsList[item.id].state = QuestState.Acquired;
         }
     }
 }

Then your scavenger hunt class

     public class Hunt : $$anonymous$$onoBehaviour
     {
         public static Hunt Instance
         {
             get
             {
                 if (_instance == null)
                 {
                     _instance = FindObjectOfType<Hunt>();
                 }
                 return _instance;
             }
             private set {_instance = value;}
         }
         private static hunt _instance;
         public ScavengerHuntItem[] items;
         private Dictionary<int, ScavengerHuntItem> itemsList;
         void Awake()
         {
             Instance = this;
             itemsList = new Dictionary<int, ScavengerHuntItem>();
             foreach (ScavengerHuntItem item in items)
             {
                 itemsList.Add(item.id, item);
             }
         }
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Habitablaba · Jun 30, 2017 at 10:41 PM

One idea is to use a few enums to take care of this.

 public enum QuestState
 {
      not_started,
      started,
      complete
 }
 
 public enum Quest
 {
      find_spider,
      find_letter_opener,
      find_wine_bottle,
      find_dirty_sock,
      ... etc....
      total
 }
 
 // elsewhere...
 public QuestState[] Quests;
 // elsewhere...
 Quests = new QuestState[Quests.total]();


Then you could just use the Quest enum to index into the QuestState array and change the value to whatever state you needed for each individual quest.

Disclaimer: This is completely untested and barely thought through. It spans several files and I'm not even sure it will work, but it is an idea.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KnightRiderGuy · Jun 30, 2017 at 10:55 PM 0
Share

Thanks @Habitablaba,

This looks potentially awesome man :) Hmmm I've not had a lot of experience with those, I've heard of them but have never played with them much.... any chance you can expand on this a little it sounds like it's exactly what I might need.

I have 13 items on my scavenger hunt list. $$anonymous$$y first initial thought was to upon closing the app just have the bool get change to false in the player prefs....

But I was thinking more while the app is running, keeping track of it that way. So that if you found the spider, but for some strange reason, for which in my case there could be many, were to say you were looking for it again, then have my A.I. say something like: Why are we doing that again when we already have found the rubber spider. ;)

avatar image Habitablaba KnightRiderGuy · Jun 30, 2017 at 11:11 PM 0
Share

An enum can be thought of as aliasing a number. Every entry into the enum is just adding 1 to the previous value, unless explicitly told otherwise. So

 public enum Numbers
 {
      zero,
      one,
      two,
      three,
      four
 }

will give you Numbers.zero, Numbers.one, Numbers.two, Numbers.three, and Numbers.four, which will have the values 0, 1, 2, 3, and 4, respectively.

Ins$$anonymous$$d of an array of QuestStates, you could have an array of ints. and every time a spider is found, you could increment the value in the array at the index Quest.find_spider (or whatever). This way you can do a check to see if the value is > 0 and give the use a message about how they've done this already. You could even make the message change depending on how many spiders the user found

avatar image KnightRiderGuy Habitablaba · Jul 01, 2017 at 01:13 AM 0
Share

Thanks @Habitablaba, I think I get where you are going with this, so if I did something like:

 public enum Spiders
 {
 1,2,3,4,5,
 }

And then referenced it like this:

 if (Spiders >0)
 {
 Debug.Log("You Already Found A Spider");
 }

Am I getting that right or am I missing something? The item or items only needs to be found once, but the possibility of either deliberately or accidentally mentioning you found the spider can happen often though.

avatar image KnightRiderGuy · Jul 01, 2017 at 12:29 PM 0
Share

@Habitablaba I suppose to I might be able to do:

 public enum RubberSpiderQuestState
  {
       not_started,
       started,
       complete
  }

And then in my if statements that are checking the state do this?

 if (RubberSpiderQuestState == complete)
 {
 Debug.Log("You Already Found The Spider, Why Are You Looking For It Again?");
 }


And if I'm understanding you right I would set the sate with something like:??

I tell my A.I. "Hey pal, it looks like we found the Rubber Spider"

 RubberSpiderQuestState = complete;


Does that seem right?

avatar image KnightRiderGuy · Jul 01, 2017 at 02:23 PM 0
Share

Ah, you'll have to forgive my ignorance with these, I've never used them before... I think if I'm understanding it right, I would set up my enum like this:

     private enum RubberSpiderQuest
     {
     not_started
     started
     complet
     }
 private RubberSpiderQuest myRubberSpiderQuest;

And then for checking that state I would use:

 if (myRubberSpiderQuest == RubberSpiderQuest.complete)
 {
 Debug.Log("You Already Found The Rubber Spider");
 }

And then to set the state I would use:

 RubberSpiderQuest = complete;

Does that seem correct? Anyone? $$anonymous$$ay just require some experimentation I guess. :/

avatar image Habitablaba · Jul 05, 2017 at 06:24 PM 0
Share

Hey @zentaiguy

making an enum called Spiders is not what I was trying to drive you towards. You would have an enum that would be a collection of all the things you are trying to find in your game, and then a second enum that would track if the player had found that item or not. (and the second version where you keep track of the number of times they've found that item).

So lets say you want the user to find a spider, a chicken, a spoon, and a chainsaw. Your first enum would look something like this:

 public enum ObjectsToFind
 {
     spider,
     chicken,
     spoon,
     chainsaw
 }

Now lets assume you want to keep track of how many times they've found a item. You'd have an array in another script (whatever script you're using to keep track of these things; a player controller, or a game manager...). It might look like this:

 public int[] FoundObjects;

Then, when you find an item, let's say in this case, that they found a spider:

 FoundObjects[ObjectsToFind.spider] += 1;

Now, you can look at the value in that index and check to see if it is greater than one. If it is, then they have found the spider already and you can send them a message.

Does this make sense to you ?

avatar image KnightRiderGuy Habitablaba · Jul 05, 2017 at 07:09 PM 0
Share

@ Habitablaba, yes that does make perfect sense, although The method of using: not_started, started and complete seemed to work pretty good, that way I can track each items state. I guess both work :)

avatar image Habitablaba KnightRiderGuy · Jul 05, 2017 at 09:06 PM 0
Share

yep, both are good. The argument I have for the int[] ins$$anonymous$$d of the status enum is that you can then change the message to the user as they find more of the same object.

If they've found the spider twice, you can tell them "Hey man, you did this already, go do something else." but if they found the spider five times, you can tell them "Hey man, seriously stop trying to find the spider, you got a big long list of stuff to find and the spider is only one thing on it. What are you, spider man?!?"

whereas if all you track is state, then all you know is that they've found it before.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

340 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I check if a Bool was True a few moments ago 2 Answers

Could use some help on making my runaway/chase/follow/patrol script cooperate with each other 1 Answer

c# Ignoring conditional statement? 1 Answer

How can i reverse all the booleans in a method? 2 Answers

my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges