#score

1 messages · Page 1 of 1 (latest)

molten estuary
silent steppe
#

Use ``` at start and the end of code

molten estuary
#

public Text Myscoretext;
    private int ScoreNum;
    // Start is called before the first frame update
    void Start()
    {
        ScoreNum = 0;
        Myscoretext.text = "Score : " + ScoreNum;
    }

    private void OnTriggerEnter2D(Collider2D Asteroid) 
    {
        if (Asteroid.tag == "Asteroid") 
        {
            ScoreNum = + 1;
            Destroy(Asteroid.gameObject);
        }
    }

    void Update()
    {
        Myscoretext.text = "Score: " + ScoreNum;
        Debug.Log(ScoreNum);
    } 
harsh sparrow
#

+= 1

#

not =+ 1

molten estuary
#

yeah i did that

harsh sparrow
#

no you didn't

#

you did ScoreNum = + 1;

#

you need to do ScoreNum += 1

molten estuary
#

no =+ was me attempting to rewrite it

harsh sparrow
#

why are you rewriting instead of copy pasting..

molten estuary
#

i did += originally

harsh sparrow
#

now you're wasting time presenting issues that aren't even there

#

copy paste your exact code

molten estuary
#

public Text Myscoretext;
    private int ScoreNum;
    // Start is called before the first frame update
    void Start()
    {
        ScoreNum = 0;
        Myscoretext.text = "Score : " + ScoreNum;
    }

    private void OnTriggerEnter2D(Collider2D Asteroid) 
    {
        if (Asteroid.tag == "Asteroid") 
        {
            ScoreNum += 1;
            Destroy(Asteroid.gameObject);
        }
    }

    void Update()
    {
        Myscoretext.text = "Score: " + ScoreNum;
        Debug.Log(ScoreNum);
    } 
#

there

#

exact code

harsh sparrow
#

when does it reset to 0?

molten estuary
#

after it goes to 1 it immedietly resets

harsh sparrow
#

do you assign to ScoreNum anywhere else?

molten estuary
#

no

harsh sparrow
#

how do you know it's resetting? the Debug.Log says 0?

molten estuary
#

yes as well as i have score: on the screen

harsh sparrow
#

do you have any errors in your console?

silent steppe
#

The code looks fine

molten estuary
#

yeah there's 1 error but i don't think it's causing the problem

molten estuary
#

cause it's just a clone being delted

molten estuary
pearl relic
#

Oh, you're trying to destroy a prefab, rather than an instance of a prefab

harsh sparrow
#

that's you trying to delete an asset instead of a clone of a prefab

molten estuary
#

could it be the cause?

silent steppe
#

Is the prefab the asteroid or I am dumb and not able to understand the error

molten estuary
#

the prefab is the asteroid

silent steppe
#

And the code says if its destroyed only then the score can be 1

#

That's the problem

molten estuary
#

ahh okay

#

how do i destroy the clone instead of the prefab?

pearl relic
#

Is it actually an error related to this script?

#

Because you're destroying the thing that enters a trigger, but that should be an instance of a prefab

silent steppe
pearl relic
#

But he has no reference to the prefab in this class

molten estuary
#

well a clone of the prefab

silent steppe
pearl relic
#

Exactly, so is this is the script the error is warning about?

molten estuary
#

yeah now i am asking how do i delete the clone?

#

yes

#

this is going off of my question tho

silent steppe
#

Nvm you didnt even give reference to the prefab

pearl relic
#

Heaven forbid we try and go onto a different but related issue

molten estuary
#

sorry

silent steppe
#

@pearl relic offtopic but I really want to say thanks to you

molten estuary
#

I am really sorry

silent steppe
#

When I was a beginner at my old I'd you were the one always helping me@pearl relic

#

And now I am here helping others I really want to say thanks to you

pearl relic
#

We haven't seen any spawning script, we haven't even got this class as a full script

#

So really, we're at a loss here

molten estuary
#

I am on a small amount of sleep and I am not properly thinking sorry.

molten estuary
#

if you want

pearl relic
#

Aye wheel it out then

silent steppe
#

@molten estuary also can you send the object hierarchy

molten estuary
#
{
    public Transform asteroidPoint;
    public GameObject asteroidPrefab;
    void Start()
    {
        StartCoroutine("Reset");
    }

    void Update()
    {
    }

    IEnumerator Reset()
    {
        GameObject asteroid = Instantiate(asteroidPrefab, new Vector2(Random.Range(-5f, 5f), 4f), asteroidPoint.rotation);

        //yield on a new YieldInstruction that waits for 0.1 seconds.
        yield return new WaitForSeconds(Random.Range(0.5f, 1f));

        Destroy(asteroidPrefab);


        StartCoroutine("Reset");
    }
}```
#

spawning script

pearl relic
#

See

molten estuary
#

trying a new deltion

pearl relic
#

This is the script the error is for

silent steppe
#

He is destroying the prefab

molten estuary
#

i think i may have figured out how to delete it maybe

pearl relic
#

Bingo

silent steppe
#

Its clearly written in code

molten estuary
#

yeah now i see the issue

pearl relic
#

Well now that is out of the way, back to your 0s and 1s

#

Are there multiple instances of the score script in the scene?

silent steppe
#

@molten estuary can you send the object hierarchy once

pearl relic
#

Also, do we know for sure that the OnTriggerEnter2D and the subsequent if statement are both run?

molten estuary
#

ahh

#

i got it

#

the bullet is being cloned

#

and so all of them are resetting it

#

oh my god

#

I am just being an idiot sorry

silent steppe
#

No probs lmao