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 werdna_90 · Mar 18, 2016 at 10:05 PM · scripting problem

My Instantiated enemies do not add there score to the score counter. why?

I am creating a 2D Asteroids game but I seem to be having trouble getting my score counter to work. I am very new too programming and I do not even know how to explain what I am trying to fix. My goal is to spawn enemies that when are hit by my bullets they are destroyed from the scene and a number of points should be added to my score counter. It works fine for the first asteroid that spawns. The score goes up by 1 point then if a UFO spawns next and I hit it with a bullet it is destroyed also but the UFO points that should be added are 10 but the score reads 10 when it should read 11 like as if it resets and then adds the 10 points. Then if I hit another Asteroid after hitting a UFO it reads 1 point again on the score counter. Then if I hit another UFO after the Asteroid it reads 10 again but if I hit a UFO after another UFO the score still say 10 when it should read 20 and if I hit an asteroid after another asteroid the score stays at 1 when it should read 2. I know there is something very simple wrong with my scripts but I am totally confused and if I remove the destroy(game object) from each script when I keep hitting the same UFO the score rises by ten points the way I want it too but the UFO is still on the scene obviously and that is not what I want. I want it to be deleted from the scene at this point. The same with the asteroid if I remove Destroy(gameObject) from its script the score rises by 1 but only on the first asteroid and UFO the others do not add any points but if I hit them they still still read add point in the console window as I am using Debug.Log to test everything. The first script below is my Destroy on collision class it is just holding some attributes that will be inherited from my other scripts

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

public class DestroyOnCollision : MonoBehaviour { public Text Score; public int count;

 public void SetScoreText()
 {
     Score.text = "Score: " + count.ToString();       
 }
 

}

Below is my Set Score On Start function that inherits the destroy on collision classes attributes.

public class SetScoreOnStart : DestroyOnCollision

{ void Awake() { count = count + 0; SetScoreText(); }

}

And then I have two scripts one for each enemy to handle when points are added and how many points are added also they handle how and when my enemies will be destroyed from the scene.

Asteroid Destroy Script

using UnityEngine; using System.Collections;

public class DestroyAsteroid : DestroyOnCollision {

     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Bullet")
         {
             count = count +  1;
             SetScoreText();
             Debug.Log("Add Point!");
             Destroy(gameObject);
             Debug.Log("Destroy Asteroid!");
     }          
 }      

}

Destroy UFO Script

using UnityEngine; using System.Collections;

public class DestroyUFO : DestroyOnCollision {

 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.tag == "Bullet")
     {
         count = count + 10;
         SetScoreText();
         Debug.Log("Add Point!");
         Destroy(gameObject);
         Debug.Log("Destroy UFO!");
     }

 }

}

Any help would be appreciated as this is the last thing I need to fix to complete the game I am very new to c# so please do not make fun if I sound like a noob because I am. I hope you guys can understand what I am trying to achieve and the way I have explained everything if you need more information please let me know.

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

FixedUpdate and Update not working in script 0 Answers

How can I load sprite from previous scene? 1 Answer

Unity 3D: Third person movement and mouse camera control 0 Answers

How to find match in 4 in a Row Game. 1 Answer

Why isn't my player moving down? 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