My portal script is not updating the information to the guiScript I try to access to.
As you can see I’m trying to access the relics from guiDisplay, I thought it went well because I don’t see any errors.
But when I play my game and put the relics on 5 in the guiDisplay, so I can open my portal.
It doesn’t print anything, probably because it can’t see that the relics on guiDisplay is 5. anyone knows what’s wrong?
public class portalScript : MonoBehaviour {
int relicsVar;
void Start()
{
transform.Find("entrance").gameObject.SetActive (false);
relicsVar = GameObject.Find("Player").GetComponent<guiDisplay> ().relics;
}
void Update ()
{
if (relicsVar == 5 && Input.GetKeyDown("o"))
{
transform.Find("entrance").gameObject.SetActive (true);
print ("portal open");
}
}
}
You are only setting relicsVar in start. So you need to get the value of relics in update before you check the value (in which case you should store a reference to your player instead of using gameobject.find in update.
So either you get the value right before your update, or you split your if and have your input.getkeydown check first, then if that is true, grab the value of relics and then check if it’s 5. (probably the way I would do it)
But while we’re at it, let’s get rid of GameObject.Find - I can guarantee you it’s going to cause issues for you someday in the future. In this case, I think a singleton for your guiDisplay class would be the best way to go.
I don’t know what I’m doing wrong.
I make a variable int and give a reference to ammo to hold, so he gets the ammo from guiDisplay.
I put this function(gunElements) in Update function when I shoot I say ammo -1, put still nothing happens.
The ammo on guiDisplay is not affected, what I need to do?
void gunElements()
{
int ammo;
ammo = GameObject.FindWithTag("Player").GetComponent<guiDisplay> ().ammo;
if (canFire == true)
{
// Shoot the bullet on keyHold
if (Input.GetMouseButton (0) && Time.time > nextFire)
{
Fire ();
ammo -= 1;
nextFire = Time.time + fireRate;
StartCoroutine (ShotEffect ());
}
}
if (gunDamage > 100) {
gunDamage = 100;
}
}
Ints are not passed by reference, they are passed by value. So when you get the value (let’s say 30) and then you assign it to an int in another script, you now have a copy of it, not a reference back to the original.
You should create a reference to your guiDisplay, Or, you might even be able to make it a singleton. But the reference will work. Then you should modify the guiDisplay ammo value or update it. Either way, as you have it not, you’re not modifying the guiDisplay ammo amount.
I understand but the only way to create a variable is by giving a string, int, double or bool whatever.
Can you give me more detail about making ammo and passing the reference to guiDisplay?
guiDisplay gunGuiDisplay;
void Awake()
{
gunGuiDisplay = GameObject.FindWithTag("Player").GetComponent<guiDisplay> ();
}
void gunElements()
{
if (canFire == true)
{
// Shoot the bullet on keyHold
if (Input.GetMouseButton (0) && Time.time > nextFire)
{
Fire ();
gunGuiDisplay.ammo-= 1;
nextFire = Time.time + fireRate;
StartCoroutine (ShotEffect ());
}
}
if (gunDamage > 100) {
gunDamage = 100;
}
}
Just a quick edit, but should give you an idea of how it works. I can’t say if this is the best design as I don’t know how your game is setup, but hopefully you understand getting the reference to the guiDisplay and not to the int itself.