• 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
0
Question by john 9 · May 06, 2011 at 11:44 AM · score

Score script player dissapearing

Hi there could someone please let me know what is wrong with this script. When i start the game my character just dissapears also my score automaticly starts at 5 and when i have it attached to the colectable object and i collide with more than 1 it never adds more score

var addpoints = 0000; var healthsound : AudioClip;

function OnTriggerEnter (other : Collider) {

if (other.gameObject.tag == "collect"); addpoints += 0005; Debug.Log ("Score = " + addpoints); Destroy(gameObject); AudioSource.PlayClipAtPoint(healthsound, transform.position);

}

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 Aldwoni_legacy · May 06, 2011 at 12:37 PM 0
Share

to which gameobject this script is attached?

avatar image john 9 · May 06, 2011 at 05:02 PM 0
Share

The script was attached to the player and that is when he was dissapearing. Then i put it on my collectable object and then it would work but not add the score up (ie/collect add5 collect nothing) also the score was starting at 5 not 0

3 Replies

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

Answer by burgunfaust · May 06, 2011 at 12:01 PM

Can you see the character in the scene view or the camera view. When you press play only the camera matters. The script you posted has nothing to do with your character disappearing.

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 john 9 · May 06, 2011 at 04:48 PM 0
Share

No i cant see my character when the game starts ideally what i would like to happen is when i collide with my object i get 5 points it destroys the object and also displays this on screen but with custom graphics ins$$anonymous$$d of unity's gui.Text.text

avatar image burgunfaust · May 07, 2011 at 12:23 AM 0
Share

You might have to orient your camera to point at your character. It sounds like you can only see your character in the scene view. If you select your main camera in the hierarchy, it will show you a window of what the camera sees. That's how you can tell.

avatar image
0
Best Answer

Answer by loramaru · May 07, 2011 at 03:17 AM

First, a cleaned up formatting of your code...

var addpoints = 0000; var healthsound : AudioClip;

function OnTriggerEnter (other : Collider) { if (other.gameObject.tag == "collect"); addpoints += 0005; Debug.Log ("Score = " + addpoints); Destroy(gameObject); AudioSource.PlayClipAtPoint(healthsound, transform.position); }

I'm not certain why the OnTriggerEnter() is going off right away (some other trigger near the player) but your oddities come from a pair of bugs within it.

Your 'if' statement is followed by a semi-colon rather than a open brace (thus immediately ending the conditional block) so the entire function will be called regardless of what trigger it collides with. Secondly your Destroy() call is actually destroying the object that the script is attached to, i.e. the player. Since your have not qualified the gameObject variable your are passing to it, you are actually passing this.gameObject.

What you want is...

function OnTriggerEnter (other : Collider) {
    if (other.gameObject.tag == "collect") {
        addpoints += 0005;
        Debug.Log ("Score = " + addpoints);
        Destroy(other.gameObject);
        AudioSource.PlayClipAtPoint(healthsound, transform.position);
    }
}

Now it should only add points when a "collect" object collides with it and it will no longer destroy the player object.

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 john 9 · May 07, 2011 at 04:48 PM 0
Share

Thanks that works but now the score automaticly starts at 5 so when i collect my first object it Debug.Log's 10

avatar image john 9 · May 07, 2011 at 04:50 PM 0
Share

Also how do i add a custom font or gui texture so that when i collide it adds my texture ie Angry birds score font or simalir.

avatar image loramaru · May 07, 2011 at 06:04 PM 0
Share

Then check your scene; you must have a "collect" object that is starting in contact with the player and being immediately collided.

Regarding the font topic, please ask additional topics in their own question so that they can be viewed/answered/indexed appropriately.

avatar image john 9 · May 07, 2011 at 09:43 PM 0
Share

I sorted it mate All i can say is thanks very much

avatar image
0

Answer by john 9 · May 07, 2011 at 09:44 PM

Answered Thanks loramaru

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.

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

No one has followed this question yet.

Related Questions

how to make score number run 2 Answers

Keeping score problems dontdestroyonload 2 Answers

help with score display concept 2 Answers

Referencing Scripts properly and still not working? Help! 1 Answer

How do I fix this Null Reference error and update my score? 2 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