- Home /
can't change variable through other script
I am making a game about planes and I having some problem with inheritance First I have created varible called "YükseltmeHakkı" (UPGRADING RIGHT):
using UnityEngine.UI;
public class UpgradeRemainder : MonoBehaviour {
public int YükseltmeHakkı;
public void Update () {
YükseltmeHakkıText.text = "Kalan yükseltme hakları: " + YükseltmeHakkı;
}
Then I called this variable from anaother script in this function:
public void UpgradeDamage()
{
if(upgradeRemainder.YükseltmeHakkı > 0)
{
Damage += 5;
upgradeRemainder.YükseltmeHakkı--;
}
Normally,this code supposed to work when I presssed the button (decrease YükseltmeHakkı). But when I pressed the button that script is assigned to it does nothing (even when YükseltmeHakkı is bigger than 0).
Why could this be happening and how could I overcome this problem please help.
do not cut pieces of code out of their context because then you're cutting out possibilities as to what the problem might be caused by and people won't bother answering.
The code upgradeRe$$anonymous$$der declared and YükseltmeHakkı was called.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnPlanes : $$anonymous$$onoBehaviour {
// Update is called once per frame
[Header("Uçak")]
public Transform Spawnpoint;
public Rigidbody Uçak;
public bool Uygun$$anonymous$$u;
public float WaitTime;
[Header("Saldırılar")]
public SpawnInWaves spw;
public UpgradeRemainder upgradeRemainder;
public int Sayı;
public int SayıArttır = 20;
public void Update()
{
if (Uygun$$anonymous$$u == true)
{
Instantiate(Uçak, Spawnpoint.position, transform.rotation);
Sayı -= 5;
Uygun$$anonymous$$u = false;
StartCoroutine(SetTrue());
if(Sayı == 0)
{
Sayı = SayıArttır;
spw.Wave++;
upgradeRemainder.YükseltmeHakkı++;
SayıArttır += 5;
}
}
}
IEnumerator SetTrue()
{
yield return new WaitForSeconds(WaitTime);
Uygun$$anonymous$$u = true;
}
This code that Yükseltme Hakkı declared
using System.Collections;
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UpgradeRemainder : $$anonymous$$onoBehaviour {
public int YükseltmeHakkı;
public Text YükseltmeHakkıText;
// Update is called once per frame
public void Update () {
YükseltmeHakkıText.text = "$$anonymous$$alan yükseltme hakları: " + YükseltmeHakkı;
}
}
Hope that helps to tell my problem.
I don't see any inheritance so that's most likely not the issue.
where is your
public void UpgradeDamage()
declared?
what are you actualy trying to call in your onClick listener on your button?
How is declared upgradeRemainder
? How do you get a reference to it? Do you drag & drop something in the inspector? Do you use Find
+ GetComponent
?
I drag drop something to inspector. I do not use Find
+ GetComponent
.
Answer by daggertok · Jul 15, 2019 at 12:13 PM
Please show us how you initialized UpgradeRemainder in your second script, you need to have
in second script, and assign your original UpgradeReminder to game object with second script through inspector. Or you can do this in second script.public UpgradeRemainder upgradeReminder;
upgradeRemainder = GameObject.Find("TheNameOfGameObjectFirstScriptIsIttachedTo").getComponent();
Also make sure your YükseltmeHakkı has default value which is higher than 0, otherwise its not gonna pass if() statement