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 CarrierOfTime · Jan 07, 2012 at 03:37 PM · prefabscollisionsscoringdetecting

Increase score when hit by a prefab help?

Currently i have this script:

  function OnCollisionEnter(theCollision :

Collision){

        if(theCollision.gameObject.name == "Sphere1(Clone)"){
          ScoreControl.score += 100;
          Debug.Log("Avoid the spheres!");
        if(theCollision.gameObject.name == "Sphere2(Clone)"){
          ScoreControl.score += 100;
          Debug.Log("Avoid the spheres!");
        if(theCollision.gameObject.name == "Sphere3(Clone)"){
          ScoreControl.score += 100;
          Debug.Log("Avoid the spheres!");
        if(theCollision.gameObject.name == "Sphere4(Clone)"){
          ScoreControl.score += 100;
          Debug.Log("Avoid the spheres!");
        if(theCollision.gameObject.name == "Sphere5(Clone)"){
          ScoreControl.score += 100;
          Debug.Log("Avoid the spheres!");
        if(theCollision.gameObject.FindWithTag

("bullet")) {

          ScoreControl.score += 150;
          Debug.Log("Don't get hit!");
                          }
                      }
                  }
              }
          }        
      }
  }

The last part works fine, but i am having trouble with my sphere prefabs.

Basically, they are rigid bodies that are spawned from gameobjects and they roll down a hill, what i want to happen, is when they hit my player character, for the score to increase to the above amount 100 points, but for some reason it is not working, why is this?

I made them prefabs, even tried checking "collision detection: dynamic continuous" but every time my character gets hit by the spheres nothing happens, this is the script i have attached to the player character obviously too.

What do i need to do to make it so the score increases when im hit by the spheres?

Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Anxo · Jan 07, 2012 at 04:37 PM

you are not closing your if statements in the correct place.

 if(theCollision.gameObject.name == "Sphere1(Clone)"){
 
        ScoreControl.score += 100;
 
        Debug.Log("Avoid the spheres!");
 }  <------- "this is an arrow"
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 CarrierOfTime · Jan 08, 2012 at 12:25 PM 0
Share

meant so say here that, thanks for this but it still doesnt work :S, spheres continue to roll down and hit my character without adding points, i have a roothandle and collision box on my character as its a fully animated model...does this make a difference? should i attach the script in more than one place? This is how it looks now:

http://pastebin.com/Cn9BYhg$$anonymous$$

I dont understand how to get this to work :S, i thought it would be simple lol.

avatar image Anxo · Jan 08, 2012 at 01:50 PM 0
Share

Well first, you are using the wrong function to compare tag. You are using gameObject.FindWithTag ins$$anonymous$$d of theCollision.gameObject.CompareTag("tag")

You also have to make sure that the var you are trying to access in the score control script is a static var.

It is a really simple thing to do and ins$$anonymous$$d of trying to make it work for 2 days, I highly recommend that you run through these $$anonymous$$odules first and then try again. You will be able to do it in now time and you will be able to do a lot of other things very fast.

http://www.unity3dstudent.com/category/modules/beginner/

avatar image
0

Answer by Pixelen · Jan 08, 2012 at 02:52 PM

I had this problem too, and what I did was create a separate script for scores, so in the script attached to the player you should put:

 if(theCollision.gameObject.name == "Sphere2(Clone)"){    
        gameObject.Find("Score").SendMessage("ScoreUp"); //this calls the score function    
        Debug.Log("Avoid the spheres!");
 }

 

And then you should create a different script and attach it to the score GUI, something like:

 var counter : int = 0;    
 
 function Update () {    
     guiText.text = "Score: "+counter;    
 }    
 
 function ScoreUp () {        
     counter+=100;    
 }

Hope this helps!

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 CarrierOfTime · Jan 08, 2012 at 03:18 PM 0
Share

Thanks for this but nope...still doesnt work :(. I added that script to the score GUI and i added that top script to the player character and nothing...i just dont understand why it would detect the spheres on the player character...

I currently have these 4 scripts:

1: attached to my "Player" : http://pastebin.com/TbJdhUDv 2: attacked to the "score" gui element : http://pastebin.com/caNkdjEY 3: attached to my "Player" (this works) : http://pastebin.com/DezULShg 4: attack to the "score" gui element : http://pastebin.com/LeEsBLhB

So two seperate scripts each and no luck...anymore ideas?

If not ill just give up lol.

Thanks all so far.

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

7 People are following this question.

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

Related Questions

checking two seperate objects collision 0 Answers

Is their a way to change boxcollider2d's method of detection? 1 Answer

Prefabs and collisions 1 Answer

SetActive true not working on UI object 2 Answers

Is there a way or an asset for managing assets that make up a prefab? 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