Score does not increment on collision(Maybe because its a prefab?)
Hello guys .So I have a prefab spawn at random locations in the screen and when the player collides with it it gets destroyed.Works great. I added a score so when the collision happens it gets incremented by 1. I made a debug.log on the console to see if the score works great but it gets incremented to 1 .Does not get higher than that. It detects every collision and destroyed the object but every time the console outputs 1.
Is this problem caused because the object is a prefab and is the same thing?
Answer by shaileshteli14 · Apr 12, 2020 at 04:10 AM
You haven't posted your script/code for anyone to find the problem. But still, this should work.
public class Yourscript : MonoBehaviour
{
public int score;
void OnCollisionEnter2D()
{
score++;
Debug.log(score);
}
Yea i am sorry . That's exactly my code.I just destroy the prefab on collision as well.
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
Destroy(this.gameObject);
score++;
Debug.Log(score);
}
}
You are doing it wrong. I know the reason but I won't be able to explain cause I am really bad at explaining things especially coding. But, you might understand the reason on your own once you read the solution. The solution is: Don't attach your script to the gameobject that you are instantiating, attach the script to the player. And do the following changes:
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Object")
{
Destroy(col.gameObject);
score++;
Debug.Log(score);
}
}
That's it. Either you do this or you can also do the thing that "@streeetwalker says. I just find this one easier.
Indeed it worked great on the player script. Thanks for the answer!
Answer by streeetwalker · Apr 12, 2020 at 09:09 AM
It doesn't have anything to do with prefabs. You need a global variable for score, not a variable local to every object. As it seems now, every game object has it's own score, which you do increment, but then you kill the object, so each local score never goes beyond 1.
So somewhere you need to have a "Game Controller" script that contains the score for the whole game, and you need to give each object a way to reference that score variable. That way, when you increment the score when an object is killed, it adds to the game score.
$$anonymous$$ay i ask ..
Can i have an empty game object for example called Game$$anonymous$$anager and add 2 public gameobject variables for the player and the object. What would be the script on the Game$$anonymous$$anager to check if they are on trigger Or on collision? Can i use for example gameobject1.istouching(gameobject2) ?
Yes, the common way to do such $$anonymous$$anager scripts is to put them on empty game objects.
Yes, you can add references to the objects in your game to the such $$anonymous$$anager scripts, and it is a good idea to "track" all objects in your game using variables that are contained in the $$anonymous$$anager script, whether you place them in the scene through the editor or you generate them at runtime by using Instantiate. For example, if you have a bunch of enemies that you instantiate from prefabs you store a reference to each one in an array. Same with bullets that you shoot.
In many cases, yes, you do need to also give your object scripts a reference to the $$anonymous$$anager, so that you can set a variable, such as the score, from the object. But you can do that from the manager script.
Here are a few snippets of a couple of scripts (not showing everything here of course):
// game manager script
public class Game$$anonymous$$anager : $$anonymous$$onoBehavior {
// using a dictionary is the best* option for storing a list of
// game objects that you need to track: a little more
// difficult to iterate through, but much easier to remove from
// * best for this example, in other cases lists/arrays are better
private Dictionary<string, GameObject> enemies;
private int score = 0;
private int enemyCount = 0;
private void Start {
enemies = new Dictionary<string, GameObject>();
}
public void SpawnEnemy() {
GameObject e = (GameObject) Instantiate( enemyPrefab );
e.name = "enemy_" + enemyCount++;
enemies.Add( e.name, e );
EnemyScript eS = e.GetComponent<EnemyScript>();
eS.game$$anonymous$$anager = this; // enemy reference to this script
}
public void DestroyEnemy( GameObject enemy ) {
score++;
enemies.Remove( enemy.name );
Destroy( enemy );
}
}
//
// enemy script
public class EnemyScript : $$anonymous$$onoBehavior {
public Game$$anonymous$$anager game$$anonymous$$anager;
void OnTriggerEnter2D( Collider2D col ) {
if ( col.gameObject.tag == "Player" ) {
game$$anonymous$$anager.DestroyEnemy( this.gameObject );
}
}
}
A note about the code I just posted:
There are many ways you can rig the variables and functions between the classes, but anything want to share needs to be public.
Be aware that you can only create references to Objects: such as Scripts, GameObjects, Transforms, Dictionaries and other types of list, to name a few. You cannot create a reference to a elementary variable type (e.g. float, int, bool) or string, or structures such as Vector3, Quaternions, to name a few.
For example, if you declared "public int score" in Game$$anonymous$$anager and then tried to share that with the enemy by creating a reference to it, you'd get a copy of score, not a reference to the variable. The only way for EnemyScript to access score in the Game$$anonymous$$anager script is to give EnemyScript a reference to Game$$anonymous$$anager as in the code above: game$$anonymous$$anager.score (but again, score would need to be declared public).
Amazing. This looks like a better way to program after all . Thanks for the lesson . $$anonymous$$uch appreciated!
Your answer
Follow this Question
Related Questions
C# - My increament code is not working? 1 Answer
Pick up code issue 1 Answer
Horrible Collison 2D -- 1 Answer
make sure there is no overlap when re-enabling objects 1 Answer
Enter button 0 Answers