Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by georgelabra · Apr 11, 2020 at 06:54 PM · collision2d

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
    } 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image georgelabra · Apr 12, 2020 at 08:40 AM 0
Share

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);
         }
 
     }
avatar image shaileshteli14 georgelabra · Apr 12, 2020 at 11:59 AM 0
Share

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.

avatar image georgelabra shaileshteli14 · Apr 12, 2020 at 10:07 PM 0
Share

Indeed it worked great on the player script. Thanks for the answer!

avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image georgelabra · Apr 12, 2020 at 10:08 PM 0
Share

$$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) ?

avatar image streeetwalker georgelabra · Apr 13, 2020 at 04:51 AM 0
Share

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 );
                           }
                  }
           }



avatar image streeetwalker georgelabra · Apr 13, 2020 at 05:10 AM 0
Share

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).

avatar image georgelabra streeetwalker · Apr 13, 2020 at 09:15 PM 0
Share

Amazing. This looks like a better way to program after all . Thanks for the lesson . $$anonymous$$uch appreciated!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

199 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges