- Home /
I have everything in the right place but why wont my value stay changed ?
So i have my scripts below and what is supposed to happen is a timer counts down them spawns a beam then restarted the counter and then if the beam is still spawned just restart the counter without spawning a beam then once the beam is collected by the player the value is checked and the beam is supposed to be able to be spawned again but for some reason when it comes to that the value is changing when it is called but after that goes back to normal so whats happening here ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlueTracker : MonoBehaviour {
public GameObject Beam;
public float BlueTimer;
float Due;
public int isBeamSpawn;
// Use this for initialization
void Start () {
Due = Random.Range (10, 15);
BlueTimer = Due;
}
// Update is called once per frame
void Update () {
BlueTimer -= Time.deltaTime;
if (BlueTimer <= 0 && isBeamSpawn == 0) {
Instantiate (Beam, transform.position, transform.rotation);
Due = Random.Range (10, 15);
BlueTimer = Due;
isBeamSpawn = 1;
} else if (BlueTimer <= 0 && isBeamSpawn == 1) {
Due = Random.Range (10, 15);
BlueTimer = Due;
}
}
public void BeamCheck(int spawned)
{
isBeamSpawn -= spawned;
}
}
my other script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collect : MonoBehaviour {
public GameObject Sign;
public BlueTracker blue;
// Use this for initialization
void Awake () {
blue = Sign.GetComponent<BlueTracker> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay(Collider other)
{
if (Input.GetKeyDown (KeyCode.E)) {
blue.BeamCheck(1);
Destroy (gameObject);
}
}
}
Answer by peter_kes · Mar 24, 2017 at 12:17 PM
"Input.GetKeyDown () " You need to call this function from the Update function, since the state gets reset each frame..." does this give you some hint? I am curious if Input.GetKeyDown () works in triggerstay.
I changed it around and even tried a few false variables, but if I do a "Print" for the isBeamSpawn it changes once the E is pressed but then goes back to 1 ins$$anonymous$$d of staying at 0
if (BlueTimer <= 0 && isBeamSpawn == 0) { //<-------IF "isBeam.."==0
Instantiate (Beam, transform.position, transform.rotation);
Due = Random.Range (10, 15);
BlueTimer = Due;
isBeamSpawn = 1; //<--------- "isBeam.." becomes 1
This happens in every frame.
When do you want it to be 1 and when to stay 0?
If isBeanSpawn is always 1 or 0 why don't you make it boolean? $$anonymous$$aybe you'll see thingsmore clear. Run a scholastic Debug. Put in every single line a print referring the line and isBeanSpawn value. See step by step what happens first in the code order.
Answer by PSpiroz · Mar 26, 2017 at 05:15 PM
Not sure what your saying. Please use punctuation in your sentences. What do you mean by "back to normal?" what do you want to happen and what happens exactly. Beware of what you are "Destroy" and what action / method runs first in every script.
You can also try make a method this area of script:
void doThis(){
if (BlueTimer <= 0 && isBeamSpawn == 0) {
Instantiate (Beam, transform.position, transform.rotation);
Due = Random.Range (10, 15);
BlueTimer = Due;
isBeamSpawn = 1;
} else if (BlueTimer <= 0 && isBeamSpawn == 1) {
Due = Random.Range (10, 15);
BlueTimer = Due;
}
}
And call it both in 'Update()' function and in 'BeamCheck( int spawned)'
see how it goes...
Your answer
Follow this Question
Related Questions
Classes. After creating objects. How do I find my objects by class rather than their objects? 2 Answers
Can't re-enable a gameobject 1 Answer
Count returning 0 when called outside object~ 1 Answer
Need help with assigning game objects to variable 2 Answers
GameObject1's Bool is FALSE when GameObject2 is Active? 2 Answers