Problem Activating a GameObject using a static Bool in other Scene
Hello, my problem is I want to press a certain button and when i press it, it will SetActive a GameObject in another Scene, I created two Scripts, the first when i do click on the button set True a public static bool and in the Second script I call it and if is true and the Bool is true then Active the GameObject, the problem is don't works I dont know Why.The first script is named "CargarPersonaje" contains the static bools and I want when i do click on a button to set the Bool to True and in other Scene turn On a GameObject.
Script 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CargarPersonaje : MonoBehaviour
{
public string Modo_Batalla, Modo_Batalla2;
Scene scene;
public static bool selectCharacter1;
public static bool selectCharacter2;
public GameObject char01;
public GameObject char02;
public GameObject char03;
public GameObject char04;
public GameObject char05;
public GameObject char06;
public GameObject char07;
public GameObject char08;
public GameObject char09;
public GameObject char10;
void Update()
{
scene = SceneManager.GetActiveScene();
}
public void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
if (char01.activeSelf)
{
selectCharacter1 = true;
SceneManager.LoadScene("Modo batalla2");
}
else if (char02.activeSelf)
{
selectCharacter2 = true;
SceneManager.LoadScene("Modo batalla2");
}
}
}
and the Second Script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CargarPersonaje2 : MonoBehaviour
{
public bool isPlayer1;
public bool isPlayer2;
public bool isPlayer3;
public bool isPlayer4;
void Awake()
{
if (CargarPersonaje.selectCharacter1 && isPlayer1 == true)
{
//you have been selected
gameObject.SetActive(true);
}
else if (CargarPersonaje.selectCharacter2 == isPlayer2)
{
//you have been selected
gameObject.SetActive(true);
}
else if (CargarPersonaje.selectCharacter3 == isPlayer3)
{
//you have been selected
gameObject.SetActive(true);
}
else if (CargarPersonaje.selectCharacter4 == isPlayer4)
{
//you have been selected
gameObject.SetActive(true);
}
}
}
My problem is, when i do click on the button, the GameObject don't turns On.
The button is the picture on the center.
And the GameObject needed to be activated is this:
Answer by clcriado0 · Jan 07, 2019 at 10:38 PM
@JxWolfe can you help me please?
Please, I need really help here, i will vote you all time +1
sorry, something came up...
basically, you need to have a controlling script with references to each of the gameObjects... that goes in the scene your loading. Turn them off on start by referencing your variable script if(VaribleScript.turnObject1Off) { objReference.SetActive(false); } Then you need a script with static variables
public static bool turnObject1Off` on a script in the first scene. use public void TurnOnObject1() { turnObject1Off = true;
or something along those lines for a button to call that new function.