• 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 /
avatar image
1
Question by Antao98 · Apr 16, 2018 at 07:37 PM · destroymouseclickcountclones

Keeping score when object clicked and destroyed

I'm having problems keeping score when destroying objects when they are clicked. I think its because of the clones I'm generating. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class Touch : MonoBehaviour {
 
     public GameObject pinnedRing;
     RaycastHit2D hitInfo;
     public int score;
 
     void Start()
     {
         score = 0;
     }
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             //Debug.Log ("Clicked");
             Vector2 pos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(pos), Vector2.zero);
             if(hitInfo.collider.tag == "Ring")
             {
                 score += 1;
                 Destroy (hitInfo.collider.gameObject);
                 Vector3 pinPos = hitInfo.transform.position;
                 GameObject secondRing = Instantiate (pinnedRing, pinPos, transform.rotation);
                 Destroy(secondRing, 1f);
             }
             if(hitInfo.collider.tag != "Ring")
             {
                 //Do nothing
             }
         }
         Debug.Log (score);
     }
 }

Basically, I spawn the objects and when I click them they disappear, but the score doesn't add, it just keeps on 0. How can I fix this?

Comment
Add comment · Show 4
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 sean244 · Apr 16, 2018 at 10:39 PM 1
Share

According to this code, your score only goes up if the first ring is destroyed, not the second one. Is that what you want?

avatar image Antao98 · Apr 17, 2018 at 09:11 AM 0
Share

@sean244 I want the score to go up every time a ring is destroyed

avatar image Antao98 · Apr 17, 2018 at 09:48 AM 1
Share

I just noticed that when I destroy a ring it gives the error "Object reference not set to an instance of an object", sending me to line 18 (if(hitInfo.collider.tag == "Ring"))

Show more comments

2 Replies

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

Answer by Harinezumi · Apr 17, 2018 at 10:41 AM

You need to check if the raycast hit anything at all, because if not, then hitInfo.collider will be null, and so accessing hitInfo.collider.tag will give the error you described in the comment.
In code:

 RaycastHit2D hitInfo = ...;
 if (hitInfo.collider == null) { /* do nothing */ }
 else {
     // do the same as you did originally
 }
Comment
Add comment · Show 1 · 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 GMAGames · Apr 17, 2018 at 11:26 AM 0
Share

Yes that's it

avatar image
0

Answer by GMAGames · Apr 17, 2018 at 11:16 AM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Touch : MonoBehaviour {

 public GameObject pinnedRing;
 private int score;
 Vector3 pinPos;

 void Start()
 {
     score = 0;
 }

 void Update() {

     RaycastHit2D hitInfo = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward, Mathf.Infinity);
 
     Debug.DrawRay (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector3.forward, Color.red);

     if (hitInfo.collider != null) {
         if (hitInfo.collider.tag == "Finish") {
             score += 1;
             pinPos = hitInfo.transform.position;
             Destroy (hitInfo.collider.gameObject);
             Invoke("Spawn",1f);
         }
         Debug.Log ("done");
         if (score > 0)
             Debug.Log (score);
     }
 }

 void Spawn()
 {
     GameObject secondRing = Instantiate (pinnedRing, pinPos, Quaternion.identity);
 }

}

Comment
Add comment · Show 2 · 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 GMAGames · Apr 17, 2018 at 11:17 AM 0
Share

this is work just in 2d mode

avatar image GMAGames · Apr 17, 2018 at 11:18 AM 0
Share

you must create a prefab of the ring object and tag = Finish

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

83 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy all previous clones. 2 Answers

Clones dont get removed after exiting play mode 0 Answers

Deleting all child clones in a GameObject 2 Answers

Destroying clone object in list does not remove the first one 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