Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to continue having a disabled gameobject when the scene loads?

Discussion in 'Scripting' started by Code1345, Jun 27, 2018.

  1. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Hello! I am currently creating a game that has two different scenes, that overlap with one another quite a bit. For instance, you go into the store tab, buy a shotgun, go back to the start menu, and then are able to select the shotgun to use in the level you load into. But unless you've purchased the shotgun, it stays locked in the menu, and you aren't able to use it. I've figured out how to keep the guns locked unless you buy them, and unlocked when you do, but I'm having trouble figuring out exactly how to keep them like that. What I mean by this is, every time you switch from, say, the main menu back into the store scene, the weapons become locked again. How would I be able to keep them in a continuous state of being unlocked after the player buys them? I'm not asking for a script of course, but just some pointers- like would this be best accomplished using playerperfs? Or static bools? Thanks for reading this far, and I would greatly appreciate any help!
     
  2. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    You could create a script that manages the weapons the player has bought, some sort of inventory. When the player buys a weapon, add that weapon to the inventory -a list, I imagine-. Each time your scene starts you can have a for loop that iterates over all the guns and locks each weapon that are not in the inventory, i.e, all weapons the player has not bought, and unlocks the one it has.

    Code (CSharp):
    1. foreach (Weapon weapon in store) { // I'm guessing here, it's just a pointer...
    2.     if (!inventory.Contains(weapon)){ // If the weapon is not in the inventory, i.e, the list of weapons the player bought...
    3.         LockWeapon ();
    4.     else{
    5.         UnlockWeapon ();
    6.  
    This is just one approach though, and a very blindly given one too. Perhaps we could help you more if we can check your scripts, how the buying system is set up, the store and such... Hope that little idea helps though.
     
  3. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61

    It helps a ton! The idea for the list and what it contains is a great idea, that I’m going to be sure to utilize. I really appreciate this!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Call DontDestroyOnLoad on a data storage GameObject.
     
  5. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Great idea! I had something like that in place, I just didn't put two and two together. The system works great now, thank you!