- Home /
Question by
SickTrickz832 · Jul 04, 2020 at 10:56 AM ·
c#scripting problemeditorbuild
Script works in Game View, but not in build
Hello, I have a problem with my game working perfect in the game view in Unity but once it is built it doesn't work as intended. I have a bomb which is spawned periodically and can be defused by standing in a perimeter. There are 4 possible spawn location and one is chosen at random, when the bomb spawns at the location it also turns on its corresponding perimeter in that location. The problem is is that in the game view and editor in Unity, the right perimeter is "enabled", however when the game is built the wrong one is "enabled". SEE PICTURES SHOWN
I have also put the scripts below: BombSpawner is what spawns the bombs, and BombEvent is the script attached the Bomb Object
public class BombSpawner : MonoBehaviour
{
public GameObject bomb;
public Transform[] spawnPoints;
public float bombSpawnRate;
public int spawnPointIndex;
private float bTimer;
private BombEvent bE; //Reference to the script attached to "Bomb" object.
private GameObject[] perimeters;
void Start()
{
perimeters = GameObject.FindGameObjectsWithTag("Perimeter");
foreach (GameObject perim in perimeters)
{
DisablePerimeter(perim);
}
InvokeRepeating("bombSpawn", bombSpawnRate, bombSpawnRate);
}
void bombSpawn()
{
spawnPointIndex = Random.Range(0, spawnPoints.Length); //Chooses a spawn point at random to spawn bomb at
Instantiate(bomb, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
bE = bomb.GetComponent<BombEvent>();
bE.perimeters = perimeters;
}
void DisablePerimeter(GameObject perimeter)
{
perimeter.GetComponent<BoxCollider>().enabled = false;
perimeter.GetComponent<LineRenderer>().enabled = false;
perimeter.GetComponent<PerimeterCheck>().enabled = false;
//Debug.Log(perimeter.name + "disabled. BS");
}
}
public class BombEvent : MonoBehaviour
{
public float bombDefuseTime = 10f;
public int bombTimer = 30;
public int bombDamage = 10;
public float soundVolume = 0.5f;
public float commentVolume = 3f;
public GameObject[] perimeters;
public AudioClip countdownSound;
public AudioClip explosionSound;
public AudioClip disarmedSound;
public AudioClip armedSound;
public AudioClip defusingSound;
public Vector3 soundPos;
public ParticleSystem Explosion;
private float dTimer;
private float bTimer;
private int currentDefuseTime;
private int currentBombTimer;
public int perimToEnable;
private Transform playerTransform;
private BoxCollider perimeterCollider;
private TextMeshProUGUI timerText;
private TextMeshProUGUI defuseText;
private Defusebar defuseBar;
void Awake()
{
currentBombTimer = bombTimer;
//Debug.Log("TimerST: " + currentBombTimer);
perimeters = GameObject.FindGameObjectsWithTag("Perimeter");
soundPos = GameObject.Find("Main Camera").transform.position;
AudioSource.PlayClipAtPoint(armedSound, soundPos, commentVolume);
foreach (GameObject perim in perimeters)
{
DisablePerimeter(perim);
}
}
void Start()
{
playerTransform = GameObject.FindWithTag("Player").transform; //Player Reference
if (playerTransform == null)
{
return;
}
perimToEnable = GameObject.Find("BombSpawner").GetComponent<BombSpawner>().spawnPointIndex; //Gets the spawnIndex from BombSpawner to enable the right perimeter
//Debug.Log("PerimToEnable BE: " + perimToEnable);
EnablePerimeter(perimToEnable);
if (perimeters == null)
{
perimeters = GameObject.FindGameObjectsWithTag("Perimeter");
foreach (GameObject perim in perimeters)
{
DisablePerimeter(perim);
}
}
}
public int StartDefusing()
{
if (currentDefuseTime < bombDefuseTime)
{
if (dTimer > 1)
{
AudioSource.PlayClipAtPoint(defusingSound, soundPos, soundVolume);
currentDefuseTime++;
dTimer = 0;
}
}
return currentDefuseTime;
}
private void Update()
{
timerText = GameObject.FindWithTag("TimerText").GetComponent<TextMeshProUGUI>();
defuseBar = GameObject.FindWithTag("DefuseBar").GetComponent<Defusebar>();
defuseText = GameObject.FindWithTag("DefuseText").GetComponent<TextMeshProUGUI>();
defuseBar.FixDefuseBar(currentDefuseTime, bombDefuseTime);
dTimer += Time.deltaTime;
bTimer += Time.deltaTime;
timerText.text = currentBombTimer.ToString();
defuseText.text = currentDefuseTime.ToString();
Player playerScript = playerTransform.GetComponent<Player>();
if (currentBombTimer > 0)
{
DecreaseTimer();
}
//BombExplode
if (currentBombTimer == 0)
{
playerScript.TakeBombDamage(bombDamage);
foreach (GameObject perim in perimeters)
{
Debug.Log(perim.name);
DisablePerimeter(perim);
}
ParticleSystem explosionEffect = Instantiate(Explosion) as ParticleSystem;
explosionEffect.transform.position = transform.position;
explosionEffect.Play();
AudioSource.PlayClipAtPoint(explosionSound, soundPos, soundVolume);
Destroy(this.gameObject);
}
//BombDefuse
if (currentDefuseTime >= bombDefuseTime)
{
foreach (GameObject perim in perimeters)
{
Debug.Log(perim.name);
DisablePerimeter(perim);
}
AudioSource.PlayClipAtPoint(disarmedSound, soundPos, commentVolume);
Destroy(this.gameObject);
}
}
void DecreaseTimer()
{
if (currentBombTimer > 0)
{
if (bTimer > 1)
{
AudioSource.PlayClipAtPoint(countdownSound, soundPos, soundVolume);
currentBombTimer--;
bTimer = 0;
}
}
}
void EnablePerimeter(int perimToEnable)
{
perimeters[perimToEnable].GetComponent<BoxCollider>().enabled = true;
perimeters[perimToEnable].GetComponent<LineRenderer>().enabled = true;
perimeters[perimToEnable].GetComponent<PerimeterCheck>().enabled = true;
//Debug.Log(perimeters[perimToEnable].name + "enabled. BE");
}
void DisablePerimeter(GameObject perimeter)
{
perimeter.GetComponent<BoxCollider>().enabled = false;
perimeter.GetComponent<LineRenderer>().enabled = false;
perimeter.GetComponent<PerimeterCheck>().enabled = false;
//Debug.Log(perimeter.name + "disabled. BE");
}
}
bombproblem.jpg
(257.6 kB)
Comment