Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 measton77 · Jun 28, 2014 at 05:42 PM · c#sceneboolean

How to Set a bool to Individual Scenes (C#)

I am making a game in c# and have been stuck on this for hours now. I am trying to set a bool on a "key" object to true so that once it's picked up it will disappear and not be able to be picked up again and again each time the scene is re-entered. The problem is that upon changing the bool for that one key to true, therefore destroying it after 1 key is picked up like I want, it destrouys all of the other keys in other scenes by changing their bool to true also. I was wondering if or how you could assign a bool to just one scene by having the same bool have different values depending on the level... if that makes sense. Heres the code for the destroy key- public static bool isCollected = false;

     public void Update ()
     {
     if (isCollected == true) 
         {
         Destroy (this.gameObject);
         }
     }

So if the key isCollected is equal to true, then it gets destroyed, but that destroys all keys even though that code is attached to each one individually. Here's the code for the player coming in contact with the key-

 void DeleteKey ()
     {
         DestroyKey.isCollected = true;
     }
 
     if (other.transform.tag == "Key") 
         {
         manager.keyCount += 1;
         DeleteKey ();
     }

If anything is unclear, you need to see additional code from the game, or you need more details tell me. Thanks for the help.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Juice-Tin · Jun 28, 2014 at 06:35 PM

Turn isCollected into a dictionary. Then on the gameobject, have a string with the level name that is entered in through the inspector. Each level will then have it's own key bool.

 public String levelName;
 public static Dictionary<String, bool> isCollected
 
 ------- in code
 void DeleteKey ()
 {
       isCollected[levelName] = true;
 }
 
 etc


Comment
Add comment · Show 2 · 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 measton77 · Jun 28, 2014 at 06:51 PM 0
Share

Thanks so much. I'm really new to c# and had no idea how to attach a bool to a string. Thanks again!

avatar image measton77 · Jun 28, 2014 at 07:44 PM 0
Share

The Dictionary isn't working for me, and when I look up how to do the dictionary the setup doesn't make sense for my situation. How would I set up the if statement to destroy the object?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Fading To New Scene Problem? 1 Answer

GetComponent().enabled = true; 1 Answer

Object reference not set to an instance of an object (Raycast) 1 Answer

Arrays suddenly not serializing? 2 Answers


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