- Home /
c# Disregards my bool as if it doesn't exist?
The issue is regardless of my bool canDiveBike's true or false value it still runs, almost as if it's completely ignorng that canDiveBike is a thing?
Here's my code: https://pastebin.com/VyPe664p
or
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Metal : MonoBehaviour
{
public GameObject sound;
public GameObject black;
public int hit = 0;
public GameObject na;
public GameObject parts;
public GameObject bags;
public GameObject seat;
public GameObject Wheel;
public GameObject shoes;
public bool canDiveBike = false;
public int item;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "stuffBike")
{
if (canDiveBike == true)
{
sound.SetActive(true);
}
}
/*
else if (other.gameObject.tag == "stuffMusic")
{
if (Input.GetKeyDown(KeyCode.E))
{
digMusic();
}
sound.SetActive(true);
}
*/
else
{
sound.SetActive(false);
black.SetActive(false);
}
na.SetActive(false);
parts.SetActive(false);
bags.SetActive(false);
seat.SetActive(false);
Wheel.SetActive(false);
shoes.SetActive(false);
}
void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown(KeyCode.E) && other.gameObject.tag == "stuffBike")
{
digBike();
}
/*
else if (Input.GetKeyDown(KeyCode.E) && other.gameObject.tag == "stuffMusic")
{
digMusic();
}
*/
}
void OnTriggerExit(Collider other)
{
sound.SetActive(false);
StopAllCoroutines();
black.SetActive(false);
}
/*
void digMusic()
{
StartCoroutine(DigItMusic());
}
*/
void digBike()
{
StartCoroutine(DigItBike());
}
/*
IEnumerator DigItMusic()
{
float time = 0;
float randy = 2;
while (time < randy)
{
black.SetActive(true);
sound.SetActive(false);
time += Time.deltaTime;
yield return null;
}
if (randy < 7.5)
{
Debug.Log("GUITAR");
PlayerPrefs.SetInt("pops", PlayerPrefs.GetInt("pops") + 1);
item = 1;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 2);
}
black.SetActive(false);
StopAllCoroutines();
StartCoroutine(ShowDig());
}
*/
IEnumerator DigItBike()
{
if (canDiveBike == true) {
float time = 0;
float randy = Random.Range(2.5f, 17.5f);
while (time < randy)
{
black.SetActive(true);
sound.SetActive(false);
time += Time.deltaTime;
yield return null;
}
if (randy < 5) {
Debug.Log("Nothing");
item = 0;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 0);
}
else if (randy < 7.5)
{
Debug.Log("Just some parts");
PlayerPrefs.SetInt("parts", PlayerPrefs.GetInt("parts") + 1);
item = 1;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 2);
}
else if (randy < 10)
{
Debug.Log("saddle bags");
PlayerPrefs.SetInt("bags", PlayerPrefs.GetInt("bags") + 1);
item = 2;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 5);
}
else if (randy < 12.5)
{
Debug.Log("Bike seat");
PlayerPrefs.SetInt("seat", PlayerPrefs.GetInt("seat") + 1);
item = 3;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 5);
}
else if (randy < 15)
{
Debug.Log("A Wheel");
PlayerPrefs.SetInt("Wheel", PlayerPrefs.GetInt("Wheel") + 1);
item = 4;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 5);
}
else if (randy < 17.5)
{
Debug.Log("Biking Shoes!");
PlayerPrefs.SetInt("Shoes", PlayerPrefs.GetInt("Shoes") + 1);
item = 5;
PlayerPrefs.SetInt("points", PlayerPrefs.GetInt("points") + 5);
}
black.SetActive(false);
StopAllCoroutines();
StartCoroutine(ShowDig());
}else
{
StopAllCoroutines();
}
}
IEnumerator ShowDig()
{
float time1 = 0;
float randy1 = 2.0f;
while (time1 < randy1)
{
if (item == 0)
{
na.SetActive(true);
parts.SetActive(false);
bags.SetActive(false);
seat.SetActive(false);
Wheel.SetActive(false);
shoes.SetActive(false);
}
else if (item == 1)
{
na.SetActive(false);
parts.SetActive(true);
bags.SetActive(false);
seat.SetActive(false);
Wheel.SetActive(false);
shoes.SetActive(false);
} else if (item == 2)
{
na.SetActive(false);
parts.SetActive(false);
bags.SetActive(true);
seat.SetActive(false);
Wheel.SetActive(false);
shoes.SetActive(false);
}
else if (item == 3)
{
na.SetActive(false);
parts.SetActive(false);
bags.SetActive(false);
seat.SetActive(true);
Wheel.SetActive(false);
shoes.SetActive(false);
}
else if (item == 4)
{
na.SetActive(false);
parts.SetActive(false);
bags.SetActive(false);
seat.SetActive(false);
Wheel.SetActive(true);
shoes.SetActive(false);
}
else if (item == 5)
{
na.SetActive(false);
parts.SetActive(false);
bags.SetActive(false);
seat.SetActive(false);
Wheel.SetActive(false);
shoes.SetActive(true);
}
time1 += Time.deltaTime;
yield return null;
}
na.SetActive(false);
parts.SetActive(false);
bags.SetActive(false);
seat.SetActive(false);
Wheel.SetActive(false);
shoes.SetActive(false);
}
}
Answer by Bunny83 · Sep 15, 2018 at 04:16 PM
Your variable canDiveBike is a public variable. Therefore it is serialized in the inspector. That means the field initializer in your code has no effect on the starting value. Once the field is serialized unity will use the value you see in the inspector.
Just added a start function and made it always start true. THAN$$anonymous$$ YOU!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Use a #if statement to check if a script exists in project? 0 Answers
c# Ignoring conditional statement? 1 Answer
How to make boolean be true for x amount of seconds? 2 Answers