Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

i need help with unity code

A topic by MemeAdict123 created May 11, 2021 Views: 278 Replies: 2
Viewing posts 1 to 4

hi im tryinf to make it when a object with this script get destroyed i want it to add to another script that wil ad numbers to a script

System.Collections; using System.Collections.Generic; using UnityEngine; public class destroy : MonoBehaviour {
void OnMouseDown() {
Destroy (gameObject);
} }

(3 edits)

Do you want to add to a score in another script when each instance is destroyed?

This thread ought to be in another part of the forum. But if you clarify your problem i can help.

Moderator moved this topic to General Development
(1 edit) (+1)

Hello!

Your request is a little bit confusing, but what I understand is that you may  want to have a GameObject with a script with the score (number of enemies killed perhaps?), and what you want to have is a way to keep track of how many are getting killed in that gameobject.

You can try different approaches and tricks, like search for components, link gameobjects between them, events and delegates, playerprefs...

But maybe the easiest or faster to understand is using a Singleton for a GameManager script that will keep track of the score and other elements.

Here the Singleton code page:

http://wiki.unity3d.com/index.php/Singleton

You may need to create a gameobject with a Monobehaviour to act as the GameManager:

public class GameManager: Singleton<GameManager>
{
    ...
    public int PlayerKillCount;
    ...
}

And then, just before destroying the object in that other script you have, you make a call to this PlayerKillCount variable:

...
GameManager.Instance.PlayerKillCount += 1; 
Destroy(gameobject); 
...

The best approach depends of a lot of things to consider, but if you're just looking for a place to keep the score and be able to access to it easily by other scripts, this is a solution.

If you have further questions, just shoot.

Good luck!