[Tilevania] Question on tutorial 145

I think you mean this portion:

public class StarDisplay : MonoBehaviour
{
    [SerializeField] int stars = 100;
    Text starText;

    void Start()
    {
        starText = GetComponent<Text>();
        UpdateDisplay();
    }

    private void UpdateDisplay()
    {
        starText.text = stars.ToString();
    }

Exactly.

And where does the stars value get incremented? That piece of code you pasted here is responsible for passing on the stars value to the Text component so the Text component renders the current value.

Nina,

It was already in the previous reply - why didn’t you check? I’m pasting it here again.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StarDisplay : MonoBehaviour
{
    [SerializeField] int stars = 100;
    Text starText;

    void Start()
    {
        starText = GetComponent<Text>();
        UpdateDisplay();
    }

    private void UpdateDisplay()
    {
        starText.text = stars.ToString();
    }

    public void AddStars(int amount)
    {
        stars = stars + amount;
        UpdateDisplay();

    }

    public bool HaveEnoughStars(int amount)
    {
        return stars >= amount;

    }

    public void SpendStars(int amount)
    {
        if (stars >= amount)
        {
            stars = stars - amount;
            UpdateDisplay();

        }
    }
}

Unfortunately, I still cannot see the requested Debug.Logs. Does AddStars ever get called? If so, what is the value of amount?

Check my message. I get 2 of these 3 values. All others are 0s. Does that mean something is wrong?

3
UnityEngine.Debug:Log(Object)
StarDisplay:AddStars(Int32) (at Assets/Scripts/StarDisplay.cs:24)
Defender:AddStars(Int32) (at Assets/Scripts/Defender.cs:19)

3
UnityEngine.Debug:Log(Object)
StarDisplay:AddStars(Int32) (at Assets/Scripts/StarDisplay.cs:27)
Defender:AddStars(Int32) (at Assets/Scripts/Defender.cs:19)

What does 3 mean? Is that the value of the stars variable? If so, why are there two messages? Is there more than one StarDisplay instance in your Hierarchy? Log GetInstanceID() along with a meaningful message into your console.

Update: Getinstance gives 3.

    public void AddStars(int amount)
    {
        GetInstanceID();
        Debug.Log(amount);
        stars = stars + amount;
        UpdateDisplay();
        Debug.Log(amount);
    }

The code logs amount twice into the console. What about stars?

GetInstanceID() returns an integer which needs to be logged into the console. If in doubt, look that method up in the API. There is an example.

Nina,
I’m unsure how to read the manuals. Can i just copy and paste this? See below. That means i don’t bother with any other code in the api. Can or not?

        foreach (GameObject go in allObjects)
        {
            Debug.Log(go + " is an active object " + go.GetInstanceID());
        }

Have you already tested that code?

Error: Assets\Scripts\StarDisplay.cs(24,35): error CS0103: The name ‘allObjects’ does not exist in the current context.

What do i do?

Remove the code as it obviously does not work. Then log GetInstanceID() into your console, for example, like this:

Debug.Log("Instance ID of " + name + ": " + GetInstanceID());

Update: This issue has been solved.

I get this message now:

Instance ID of Stars Text: -1122
UnityEngine.Debug:Log(Object)
StarDisplay:AddStars(Int32) (at Assets/Scripts/StarDisplay.cs:24)
Defender:AddStars(Int32) (at Assets/Scripts/Defender.cs:19)

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

Privacy & Terms