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 plumel · Mar 11, 2016 at 02:16 PM · uiprefabtextcountontriggerenter2d

Scoring system not working?

I'm trying to make it so that it will increase score by 10 when the gem is picked up by the Player gameObject. I have the Count: text on screen, but it isnt increasing by 1 when the prefab is picked up.

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

public class RandomSpawn : MonoBehaviour {

 public GameObject Gem;
 private int count;
 public Text countText;

 void Start()
 {
     count = 0;
     SetCountText ();
 }

 void SpawnGem()
 {
     Vector3 RandomSpawn = new Vector3 (Random.Range (-8.87F, 8.87F), Random.Range (-3.87F, 3.97F),1F);
     Instantiate (Gem, RandomSpawn, Quaternion.identity);
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.CompareTag ("Player")) 
     {
         count = count + 10;
         SetCountText ();
         SpawnGem ();
         Destroy (Gem);
     }
 }
 void SetCountText()
 {
     countText.text = "Count: " + count.ToString ();
 }

}

Many thanks

Comment
Add comment · Show 2
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 KdRWaylander · Mar 11, 2016 at 02:22 PM 0
Share

Where is this script ? On the player ? On the gem ? Somewhere else ?

Add a Debug.Log in your OnTriggerEnter2D (before the if statement) to see if the collision is detected.

$$anonymous$$ake sure that either the gem or the player has a Rigidbody.

$$anonymous$$ake sure that the object that collides with the object carrying the script is set as "trigger" in its collider component.

Add a Debug.Log inside the if statement to see if the condition is fulfilled.

avatar image plumel KdRWaylander · Mar 12, 2016 at 11:56 AM 0
Share

@$$anonymous$$dRWaylander , The script is on the gem. The collision is definitely detected because when I run into the gem with the Player gameobject the gem is deleted and a new one spawns in a random position, the player has rigidbody, the gem is set to trigger. After adding a debug to the count variable, I see that it counts to 1 but then no more after more gems are picked up

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Salfii · Apr 27, 2017 at 09:56 AM

I think you need to move the function "OnTriggerEnter2D" to another script and attach it to the player-Gameobject. Then you need to compare if it´s a gem, and not if it´s the player. Your random spawner propably never collides with anything because it´s not attache to the player (hopefully).

if this script is already attached to the pickup or player:

  • Does the pickup-GameObject and the player GameObject have an collider2D (which one? Edge, Box, etc?) and a rigidBody2D ?

  • Is "IsTrigger" enabled on the pickup-GO? because you´re using OnTriggerEnter2D so the collider needs to be a trigger. An alternative might be OnCollisionEnter2D() -To Which gameObject is this script attached? The Player or the pickup? Considering the inner if() of the OnCollisionEnter2D() it might be attached to the pickup but this makes no sense, because the pickup wont spawn itself via SpawnGem().....

Additional hints:

  • Consider writing an own class/MonoBehaviour for the points or GUI in generall which you call when a pickup is collected.

  • Don´t use instantiate, google objectPooling or you will have performance problems sooner or later because the GarbageCollector will go nuts than and now.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Score system not working 1 Answer

Adding Text to Prefab 0 Answers

Why does my text doesn't stay on screen? 1 Answer

Help! with the Message text 0 Answers

How can I use the text generator with the UI? 1 Answer


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