Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Crispinator · Dec 16, 2013 at 01:21 AM · scorescore systemscoreboardscoring

Scoring System

I have been looking at this script for hours and have gotten nowhere. I have two scripts. One manages the score ad is on the gameManager. The second is on the enemy. I cannot figure out what is wrong.

Score Script

     #pragma strict
     
     
     var curPoints : int;
     
     function Start () {
     
     }
     
     function ApplyPoints (curPoints : int) {
         
             Debug.Log(curPoints);
             GameObject.Find("Scoregui").guiText.text = ""+ curPoints;
     }





Enemy Script

 #pragma strict
 var gameManager : GameObject;
 
 function OnCollisionEnter (collision : Collision)
 {
 
     gameManager.GetComponent("Score");
     Component("Score").SendMessage("ApplyPoints", 10);
     Destroy(gameObject);
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FirePlantGames · Dec 16, 2013 at 01:56 AM

first of all get rid of the pragama script at the top

then use this for the enemy: var gameManager : GameObject;

 function Awake()
 {
     //so you don't have to assign the game manager in the 
     //inspector manually(also have the manager tagged as player)
     gameManager = GameObject.FindWithTag("Player");
 }
 
 
 function OnCollisionEnter(col : Collision)
 {
         //i am assuming your controller script is called    
         //Score
     if (col.gameObject == gameManager)
     {
          gameManager.GetComponent(Score).curPoints += 1;
          Destroy(gameObject);
     }
 }
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 FirePlantGames · Dec 16, 2013 at 01:34 AM 0
Share

sorry have the: var game$$anonymous$$anager : GameObject; in the script

avatar image YoungDeveloper · Dec 16, 2013 at 02:00 AM 0
Share

You should not get rid of #pragma strict if you care about optimizations. Please, don't say that he should get rid of that as a fact.

avatar image
0

Answer by YoungDeveloper · Dec 16, 2013 at 01:51 AM

I don't understand why do you need OnCollisionEnter if you don't use it, but instead manually adding the gameobject in inspector. And there is really no need to use sendmessage, as it is slower.

Enemy Script

 function OnCollisionEnter (col : Collision){   
     var scoreScript : Score =  col.GetComponent("Score");
     scoreScript.ApplyPoints(10);
 
     Destroy(gameObject);
 }

Score Script

 var curPoints : int;
  
 public function ApplyPoints (curPoints : int){
     Debug.Log(curPoints);
 }

I would not suggest using GameObject.Find either, you can get gcoregui getcomponent from score script in start function, and then use it whenever you want. I'm not an JS (unityscript) guy, but I hope i understanded what you wanted.

Also, you should add a tag check before actually getting the component, because for now, it will try to get that component from everything it touches, but as those gamobject don't have that "Score" script, you will get an null reference error.

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
avatar image
0

Answer by xandermacleod · Dec 16, 2013 at 01:53 AM

almost certainly your problem will be with collision detection and not the scripts themselves. In your OnCollisionEnter function try a basic Debug.Log() and test it to see if this is true.

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

19 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

Related Questions

How to save score for survival time? 1 Answer

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 0 Answers

Scoring help ! 1 Answer

I want my score to reset back to 0 but keep my highscore saved 3 Answers

Unity Game Score Script 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