I am able to turn off my components, but I am unable to turn them back on again. How can I fix this?
So like my question states, I can easily deactivate my components, but I cannot reactivate them. In my script, I have the ResetTriggers() called in the Start() as every trigger starts off with their BoxCollider2D and MeshRenderer deactivated. They do activate, but after the initial activation and the deactivation from the player touching it, they do not reactivate once the player has died(Touched the masklayer named death). So the code works, but only once it seems. I tried activating all of the trigger in the level(5 in total), but after deactivation from the player touching the mask they do not reactivate.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TriggerControl : MonoBehaviour {
public float Death_Wall_Speed;
[HideInInspector]
public bool IsButton;
[HideInInspector]
public bool IsTrigger;
[HideInInspector]
public bool IsWin;
public GameObject Child;
public AudioClip KeyCollected;
public AudioClip Winner;
AudioSource Source;
public bool Active;
static public bool Reset;
public bool AudioTrigger;
public BoxCollider2D collider;
public MeshRenderer MeshRend;
public BoxCollider2D Childcollider;
public MeshRenderer ChildMeshRend;
public LevelMaster levelMaster;
GameObject DM;
// Use this for initialization
void Start ()
{
//Variable setting
Active = false;
Reset = true;
Source = GetComponent(typeof(AudioSource)) as AudioSource;
DM = GameObject.Find("WorldMaster");
levelMaster = DM.GetComponent<LevelMaster>();
MeshRend = GetComponent<MeshRenderer>();
collider = GetComponent<BoxCollider2D>();
//This determines what it is.
switch (gameObject.layer)
{
case 11:
IsButton = true;
IsTrigger = false;
IsWin = false;
Childcollider = Child.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D;
ChildMeshRend = Child.GetComponent(typeof(MeshRenderer)) as MeshRenderer;
break;
case 12:
IsButton = false;
IsTrigger = false;
IsWin = true;
break;
case 13:
IsButton = false;
IsTrigger = true;
IsWin = false;
break;
}
if (Reset == true)
{
ResetTriggers();
}
}
//I am using the FixedUpdate function because the player's physics once it hits the appropriate layer pushes active to be true.
void FixedUpdate ()
{
//The levelMaster class determines the state of things around the level and when the player has died.
Reset = levelMaster.ActiveReset;
if (Active == true)
{
if (IsTrigger == true)
{
GameObject DeathWallObj = GameObject.Find("DeathWall");
DeathWall deathWall = DeathWallObj.gameObject.GetComponent(typeof(DeathWall)) as DeathWall;
deathWall.DeathSpeed = Death_Wall_Speed;
if (AudioTrigger == true)
{
levelMaster.AudioChange();
}
collider.enabled = false;
Active = false;
deathWall.IsActive = true;
}
if (IsButton == true)
{
Source.PlayOneShot(KeyCollected);
MeshRend.enabled = false;
collider.enabled = false;
Childcollider.enabled = false;
ChildMeshRend.enabled = false;
Active = false;
}
if (IsWin == true)
{
Source.PlayOneShot(Winner);
MeshRend.enabled = false;
collider.enabled = false;
Active = false;
GameObject DeathWallObj = GameObject.Find("DeathWall");
DeathWall deathWall = DeathWallObj.gameObject.GetComponent(typeof(DeathWall)) as DeathWall;
deathWall.DeathSpeed = 0;
}
}
if (Reset == true)
{
ResetTriggers();
}
}
//The Reset function
public void ResetTriggers()
{
if (IsTrigger == true)
{
collider.enabled = true;
}
if (IsButton == true)
{
MeshRend.enabled = true;
collider.enabled = true;
Childcollider.enabled = true;
ChildMeshRend.enabled = true;
Debug.Log("Working!");
}
if (IsWin == true)
{
MeshRend.enabled = true;
collider.enabled = true;
}
Reset = false;
levelMaster.ResetCleanup();
}
}
your already using logs, how far do you get with them? which get called, which don't but should. maybe another a little earlier gets called, then what happened in between. narrow it down.
P.S.: you should refactor at least a little:
make all the GetComponent(typeof...as...
GetComponent<>();
you already did that a few times.
and (boolean == true) is the same as (boolean). Or (!boolean) in case of a false check
The main problem I am having is that every Log is getting called and continues to be called but the code preceding it does not seem to work. Also I do apologize for my messy code I still think of myself as a beginner level at the very least with code. I am working on making my code cleaner and easier to read for other people. I just recently made change that told the log to print the status of each collider and renderer and it came back as true every time, but the actual object showed no change. Hence why I am stumped. Also I am slightly unsure what you mean by refactoring. I do apologize again.