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 knowles2 · Sep 10, 2014 at 10:48 AM · collisionscorecounter

Collision with cube isn't working when Instantiate clone it

Hi guys, my first question on here, I' trying to build a game where a cylinder rotates around and when it complete a rotation it adds 1 to the score. I tried doing this by adding a cube into the scene with the following script, so that when the cylinder collides with the cube it adds one to the score.

 var scoreText: GUIText;
 private var score : int;
 var counter: GameObject;
 
 function Start(){
 //counter2 = GameObject.Find("Score");
 
 
  score = 0;
  
  UpdateScore();
 }
 
 // Destroy everything that enters the trigger
 function OnTriggerEnter ( Collider ) {
     if(gameObject.tag == "Pick")
     
 //for (var x = 0; x > 1; x++){
     collider.enabled = true;
     Instantiate(counter, Vector3(42.50991, 5.041063, 34.19414), Quaternion.identity);    
 
     Destroy(counter);
         score = score + 1;
          UpdateScore();
 //    }    
         
 }
 
 function UpdateScore (){
     scoreText.text ="Count: " + score;
 }

The problem I'm having is that when the cube is instantiated it loses it collision and stops adding to the score. Any help would be appreciate, been trying to do this for a couple of days.

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 Jan_Julius · Sep 10, 2014 at 01:36 PM

Have you put your cube in a prefab? does the prefab have all the components it's supposed to have? (Collider and your script) and do you instantiate the right prefab if you have multiple ones?

Comment
Add comment · Show 3 · 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 knowles2 · Sep 10, 2014 at 02:03 PM 0
Share

Thanks for answering. I'm not using the cube as a prefab because I couldn't get the prefab to load up GUIText when it was respawn, after it was destroyed, so that the next it was destroy it would update the score GUIText , I spent 3 hours yesterday trying to get that to work and couldn't, if you think that the way I should go could you tell me how to load a prefab with GUIText after it respawn so that it can update score.

avatar image Jan_Julius · Sep 10, 2014 at 02:34 PM 0
Share

Oh I see now, this is attached to the cube, try to seperate the score into another script (try making an empty gameobject attaching the script to there) and access the script when a cube gets destroyed this means you also handle the gui in the script attached to the empty gameobject. An easy way to do this is for example if your script is called "score.cs" and you make a score variable and make that static ( allowing it to be accessed from anywhere in the game from any scene) you can simply type

 Score.score++;

whenever a cube gets destroyed in the script where the cube gets handled.

Hope this helps. :)

avatar image knowles2 · Sep 12, 2014 at 07:18 PM 0
Share

I created the score script

var scoreText: GUIText; static var score : int;

 function Start(){
  score = 0;
  UpdateScore();
 }
 
 function Update(){
  score = score+1;
   UpdateScore();
  
 }
 
 function UpdateScore (){
     scoreText.text ="" +score;
 }



Am this is my Destroy code which does destroy the cube.

 var counter : GameObject;
 // Destroy everything that enters the trigger
 function OnTriggerEnter (Collider ) {
 if(gameObject.tag == "Pick")
 
 Destroy(counter);
 Score.score++;
 }

Something wrong through that is when the cube is destroyed it doesn't update the score. An the Score script just keep on counting until infinite. $$anonymous$$anage to get the spawn to work, just got to get the three scripts working together.

avatar image
0

Answer by programmrzinc · Sep 13, 2014 at 02:39 AM

Instanitate outside the TriggerEnter ( How can a GO start a trigger if it was never created? ) use a temporary variable to hold your newly created GO ( var newGO = Instantiate(...);)

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 knowles2 · Sep 21, 2014 at 12:30 AM

Well seriously it actually came in a dream that I was being way to complicated. I didn't need to instantiate cube at all or destroy anything or spawn spawn anything.

All I needed to do to achieve what I wanted was just add the score script to a cube and then setting cube collision to trigger and counted the number of times my object spun around.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Score count increase on hit 2 Answers

Score updating after collision 1 Answer

Scoring (i'm stuck) 3 Answers

how to keep track of bricks broken 1 Answer

Score multiplier 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