How to remove a game object and replace it with a different one when all objects are gone
I'm really new to coding and only know so much, and my strategies that I've been using up to this point don't work for this issue. There are 4 objects the player is supposed to pick up, and Each object that you pick up is supposed to set a bool to false. It does do that, however my if statement doesn't seem to be even recognising that the bools are set to false and it's supposed to do something. It's probably something simple. Any explanation would be appreciated!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class AllCanisters : MonoBehaviour { public GameObject CanisterGray; public GameObject FindCanister; public GameObject SwitchButton; CanisterGrab CanisterAll;
// Start is called before the first frame update
void Start()
{
CanisterAll = CanisterGray.GetComponent<CanisterGrab>(); //Script that the bools are originally set to "true" in.
}
// Update is called once per frame
void Update()
{
if(CanisterAll.BlueC == false && CanisterAll.RedC == false && CanisterAll.GreenC == false && CanisterAll.PurpleC == false) //Doesn't seem to be doing anything but it's supposed to see if the bools are set to false since whenever you pick up an object, the bool for it gets set to false.
{
FindCanister.SetActive(false); //Remove
SwitchButton.SetActive(true); //Replace
}
}
}
Answer by rh_galaxy · May 24 at 12:08 PM
The important code is where you set CanisterAll.BlueC
(and the others both to true and then false, is it in CanisterGrab?). It is also important to know how you assign CanisterGray in the Inspector.
i have it set to where all the bools are set to true in CanisterGrab. CanisterGray is the game object with the script, and I have everything assigned in unity.
also i forgot to show it but the bools are set to true in the script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CanisterGrab : MonoBehaviour { public int layer_mask; public GameObject pedestalCanister; public GameObject hiddenCanister; public GameObject Gate;
//Objective stuff
public GameObject NoObjective;
public GameObject FindCanister;
public GameObject Blue;
public GameObject Green;
public GameObject Red;
public GameObject Purple;
public GameObject SoThatsHowItWorks;
public GameObject SwitchButton;
public bool BlueC;
public bool GreenC;
public bool RedC;
public bool PurpleC;
public Camera opCam;
public float range = 1.5f;
// Start is called before the first frame update
void Start()
{
layer_mask = LayerMask.GetMask("11");
pedestalCanister.SetActive(false);
hiddenCanister.SetActive(true);
//Objective stuff again
NoObjective.SetActive(true);
FindCanister.SetActive(false);
Blue.SetActive(false);
Green.SetActive(false);
Red.SetActive(false);
Purple.SetActive(false);
SoThatsHowItWorks.SetActive(false);
SwitchButton.SetActive(false);
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
if (Physics.Raycast(opCam.transform.position, opCam.transform.forward, out hit, range, layer_mask))
{
if(Input.GetKeyDown(KeyCode.E))
{
hiddenCanister.SetActive(false);
pedestalCanister.SetActive(true);
NoObjective.SetActive(false);
FindCanister.SetActive(true);
Blue.SetActive(true);
Green.SetActive(true);
Red.SetActive(true);
Purple.SetActive(true);
Gate.GetComponent<Animator>().Play("GateLower");
Gate.GetComponent<AudioSource>().Play();
pedestalCanister.GetComponent<AudioSource>().Play();
}
}
}
}
Here's the script.
It doesn't seem that you ever set public bool BlueC
to false, if it is true at the beginning?
Your answer
Follow this Question
Related Questions
If statement executes, even when its false again 2 Answers
How to run the same script multiple times in code but with different instances of it 0 Answers
Issue with if-statements requiring two conditions. 1 Answer
Enabling & Disabling Script via On Click 1 Answer
How to Trigger/collision only once time per gameobject hit's? 0 Answers