Search Unity

How can I activate/ deactivate objects from start of the game instead of the room through playerpref

Discussion in 'Scripting' started by heyramb89, Jun 2, 2019.

  1. heyramb89

    heyramb89

    Joined:
    Dec 29, 2018
    Posts:
    29
    Hey,

    I am using PlayerPrefs to save some objects which are going to be activate and deactivate on certain conditions.
    What's happening now is, it only works when I enter in that particular room as rooms are also getting activated through SetActive.
    To elaborate it more, see the example and code below:

    Suppose, there are 3 rooms; a, b and c. (only one room is active at a time)
    I want when I click something on Room C, an object (i) in the room B should get activated and object (ii) in the same room should get deactivated and it should get saved by Playerprefs so when I again play the game this should be intact.

    But what's happening now is, if I close the game in Room C, everything is fine but if I close the game in the Room A the progress is gone. Perhaps, this is because of void Start() which is in the Room C rather than Room A. So till I don't enter in the Room C, the progress is not getting loaded.

    Hope you understood everything.
    Please see the code below.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SaveActivateTarget : MonoBehaviour {
    6.  
    7.     public GameObject target;
    8.     public GameObject hideTarget;
    9.  
    10.     public void Start()
    11.     {
    12.         Invoke("ActivateMe", 0);
    13.     }
    14.  
    15.     public void ActivateMe ()
    16.     {
    17.         if (PlayerPrefs.HasKey(target.name)) {
    18.             target.SetActive(true);
    19.             hideTarget.SetActive(false);
    20.             return;
    21.         }
    22.         else {
    23.             //Once clicked on gameObject, target.name will get saved.
    24.             if (Input.GetMouseButtonDown(0)){
    25.                 PlayerPrefs.SetString(target.name,"showed_once");
    26.                 target.SetActive(true);
    27.                 hideTarget.SetActive(false);
    28.             }
    29.         }
    30.     }
    31. }
    32.  
    Is there anything else I can use instead of Start()?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Not sure why you're using Invoke to call ActivateMe...but besides the point.

    If these rooms are all in one scene, you should just have a script that handles what needs to be on or off.

    If they are in different scenes, you may want to have a script that loads up at the start of the game that moves over, or simply have each scene have a script that runs at the start of that scene and turns stuff off or on as needed.

    I'm not really understanding your issue since I don't really understand how you have things set up. Why does a script that turns things off or on have to be tied to a room?
     
    heyramb89 likes this.
  3. heyramb89

    heyramb89

    Joined:
    Dec 29, 2018
    Posts:
    29
    Thanks Brathnann for your reply.

    Firstly, Yes, I will remove Invoke.
    I have a RoomManager to jump through different rooms.

    Why does a script that turns things off or on have to be tied to a room?
    Ans: Because, if you see in the code, if player click on a certain object; e.g. light switch, it makes lights on (technically SetActive some images) and saves the progress by Playerprefs. That should only happen if player clicks on the switch.
    This is why I kept it in the same room. I am sorry I am beginner in the Unity.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    It's fine if you have a script in a room that controls the switches. But you should have a script that handles all your saving and loading that you can use at the start of the scene to turn things off or on. Room 1 may have a script on it that determines if it's lights are on or off. Room 2 has a script for it. etc.

    When you hit a switch, that playerpref could be something like

    Note this is a rough example
    Code (CSharp):
    1. PlayerPrefs.SetString("room1_lights", "on");
    Then when you load up the scene, you get the playerpref and handle room 1 lights as needed.

    I will say that playerprefs might not be the best use for saving game data stuff, but it may work for now while you learn.
     
    heyramb89 likes this.