Laser Defender Text error

Hello , I tried so hard (and got so far) to make a Text say “Press H to buy health” when my points are superior to 1000 and “press h to buy health or G to upgrade your damage” when my points are over 3000 . When i press H or G (if i have points) it works but the text will not be there when i reach 1000 or 3000 .
Here is my text script

   using UnityEngine.UI; 
   public class GearUp : MonoBehaviour {
    private ScoreKeeper scoreKeeper;
    private Text text;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (ScoreKeeper.score > 1000)
        {
            Text.print ("press H to buy life");
        }

        if (ScoreKeeper.score > 3000)
        {
            Text.print("press h to buy life Or G to upgrade your ship damage ");
        }
    }
}

Hi,

In your Update method you are using the types rather than your variables, e.g. ScoreKeeper instead of scoreKeeper and Text instead of text.

I am surprised you are not receiving any errors?

text.text = "Hello World!";

variable name dot object variable/property

Hope this helps. :slight_smile:

1 Like

thank you but if i write scoreKeeper instead of ScoreKeeper the game consolle sais that i need new istance or a new function :frowning:

1 Like

Ahh… Ok, so your variable score is static then.

In which case you don’t need the declaration at the top;

private ScoreKeeper scoreKeeper;

If you are still having problems, just reply again :slight_smile:

1 Like

i forgot to mention that ScoreKeeper is another script so i need to mention that .

public class ScoreKeeper : MonoBehaviour 
{
    public static int score = 0;
    private Text mytext;
    private void Start()
    {
        mytext= GetComponent<Text>();
        Reset();
    }
    public void Score(int points)
    {
        score += points;
        mytext.text = score.ToString();
    }
    public static void Reset()
    {
        score = 0;
    }
}

this is the ScoreKeeper script .

now it works . I changed the ScoreKeeper script by adding a new integer that is equal to “points” (i’ve called it punti), and changed the GearUp script with ScoreKeeper.punti
(punti is the Italian word for points)

Perhaps you could post the code again? Not really sure what you mean.

Glad you have it working though. :slight_smile:


See also;

> ...
>        >  public class ScoreKeeper : MonoBehaviour {
> >             public static int score = 0;
> >             private Text mytext;
> >             public int punti =score;
> >             private void Start()
> >             {
> >                mytext= GetComponent<Text>();
> >                 Reset();
> >             }
> >             
> >               public void Score(int points)
> >             {
> >                 score += points;
> >                 mytext.text = score.ToString();
> >             }
> >              public static void Reset()
> >             {
> >                 score = 0;
> >             }
> > ... 

and this is my new GearUp script:

 public class GearUp : MonoBehaviour {
    private Text text;
    private ScoreKeeper scoreKeeper;
	// Use this for initialization
	void Start () {
        Text text = GetComponent<Text>();
	}
	// Update is called once per frame
	void Update () {
        if (ScoreKeeper.punti > 1000)
        {
            text.text= ("premi H per comprare vita");
        }

        if (ScoreKeeper.punti > 3000)
        {
            text.text=("premi H per comprare vita o G per migliorare il danno ");
        }
    }
}

This is presumably not work @Francesco_Jack_Greco.

Can you state what the problem is you are having.

the text was not showing when my points were greater than 1000 or 3000 . but now it does :slight_smile:

1 Like

Great to hear the problem is now resolved, what was the solution in the end, so we can mark this as resolved?

1 Like

I just changed the if statement with the && and function so now it’s like this
if (ScoreKeeper.score > 1000 &&ScoreKeeper.score<3000)
and
if (ScoreKeeper.score > 3000 )
so now there is one true and one false (if i had 3000 point in the first code the first if was true and the second too so unity can’t change the text) .

1 Like

Thanks for updating the topic with your solution :slight_smile:

I’m going to change 3000 with int price and if I buy an upgrade the player should pay more everytime

>      public void Starshipdamage()
>             {
>                 price = price * moltiplicatore;
>                 scoreKeeper.Score(-prezzo);
>                 moltiplicator ++;
>             }

and in the laser script

public void Update()
    {
        moltiplic = PlayerController.moltiplicator;
        damage = moltiplic * damage;
    }

so the price will rise and the damage will rise too :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms