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 /
avatar image
0
Question by Eli-Davis · May 21, 2011 at 05:22 PM · collisiondestroyrandomtaghealth

Creating a Asteroid health script

I know this is very simple script, but considering my progress, I only downloaded unity A week ago, and with that began learning javascript

any way. what i want this to do is if the object this script is attached to is hit by an object with the tag "PlayerGun" Health is decreased. and if the health hits 0, or below 0, then the object the script is attached to is destroyed.

Also when the object is destroyed, it spawns 1 of the 5 transform variables I have below at random where the object the script is attatched to was, and If it happens to be babyAsteroid, then it spawns 3 of them. The babyAsteroid variable should have the likability of being spawned 50%. I put in what my code is below, but it doesnt work, and some parts I dont even know where to begin.

 //asteroid script =3  
 var health = 10.0;  
 var babyAsteroid:Transform;  
 var Money:Transform;  
 var addammo:Transform;  
 var shield:Transform;  
 var random:Transform;  
 
 function OnCollisionEnter( hit : Collider ){  
     if(hit.gameObject.tag == "PlayerGun") {  
     health -=1;  
     }  
 }  
   
 function Awake(){  
     if(health < 0){  
     Destroy(transform)  
     //then spawn one of the variables that are transforms. or if its babyAsteroid. 3   of them.   
     }  
 }  


Comment
Add comment · Show 5
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 Uzquiano · May 21, 2011 at 05:59 PM 0
Share

Hi EliDavis, when you post your code it looks much more clear if you mark it as code ;) it is the button with 0s and 1s

avatar image Eli-Davis · May 21, 2011 at 06:02 PM 0
Share

thank you

avatar image Eli-Davis · May 21, 2011 at 06:02 PM 0
Share

thank you

avatar image Eli-Davis · May 21, 2011 at 11:19 PM 0
Share

I'm getting an error message

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

So what should I change up? function OnCollisionEnter( hit : Collider ){ // Debug.Log("OnCollisionEnter:"); if(hit.gameObject.tag == "PlayerGun") { Debug.Log("Hit"); health --; } }

avatar image Eli-Davis · May 21, 2011 at 11:19 PM 0
Share

I'm getting an error message

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

So what should I change up? function OnCollisionEnter( hit : Collider ){ // Debug.Log("OnCollisionEnter:"); if(hit.gameObject.tag == "PlayerGun") { Debug.Log("Hit"); health --; } }

5 Replies

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

Answer by Luke Briggs · May 21, 2011 at 11:50 PM

I think this is what your looking for :)

var onDieObjects:GameObject[]=new GameObject[5];

var health:float=10f;

function reduceHealth(by:float){ health-=by; //hasn't always got to be 1 this way!

 if(health<=0){
 
 Destroy(gameObject);//destroy the object this scripts stuck to.
 
 var numberOfObjects=onDieObjects.length;
 
 var rand:int=Random.Range(0,numberOfObjects-1);//choose a random object
 
 if(rand==0){//baby rock! make it 3 times by doing a lil loop.
 
 //because this uses the same position/rotation theyll end up ontop of each other.
 
     for(var i=0;i<3;i++){
     
         Instantiate(onDieObjects[0],transform.position,transform.rotation);
         
     }
 
 }else{
 
 Instantiate(onDieObjects[rand],transform.position,transform.rotation);
 
 }
 
 }
 

}

function OnCollisionEnter( hit:Collision ){

 if(hit.gameObject.tag == "PlayerGun") {
 
     reduceHealth(1); //hits a 1.
     
 }
 

}

The above has been tested out and should do what your looking for. In the inspector you'll see an on die objects option with a drop down. Click that and then select the objects you'd like to generate, with the baby rock one in first :)

Hope that helps!

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 GesterX · May 21, 2011 at 06:51 PM

Your code is fine except you should do the health checking part in the Update function not the Awake function. The awake function is called just once - when the object is created. The Update function is called every frame. So, because you want to always be checking the health of the gameobject use the Update function. As for your spawning a baby asteroid (nice idea) you could do something along these lines:

 function Update(){
     //if health is less than or equals 0  
     if(health <= 0){
       //Generate a random number of 1 or 2
       var rand = Random.Range(1,3);
 
       if (rand == 1)
       {
         //spawn a baby asteroid
         Instantiate(babyAsteroid, transform.position, transform.rotation);
       }
     //destroy the gamObject
     Destroy(gameObject)   
     }  
 }

That should get you started.

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 Muzz5 · May 21, 2011 at 06:55 PM 0
Share

Posted whilst I was typing. Grrrr. Anyway, just combine the two.

avatar image
0

Answer by Muzz5 · May 21, 2011 at 06:54 PM

 //asteroid script =3  
 var health = 10.0;  
 var babyAsteroid:Transform;  
 var Money:Transform;  
 var addammo:Transform;  
 var shield:Transform;  
 var random:Transform;  
 
 function OnCollisionEnter( hit : Collider ){  
     if(hit.gameObject.tag == "PlayerGun") {  
     health --; 
 // -- is shorthand for -1. ++ is for +1.
 
  
     }  
 }  
 
 function Update(){  
 // update detects every frame
     if(health < 0){  
     Destroy(GameObject)  ;
 // need to add semi-colons at the end of each line (unless it has a '{')
     Instantiate (Money, transform.position, transform.rotation);
 // Use a similar thing to make the others.
 
    
     }  
 }  

I haven't tested this, so there may be a few small errors. Try and sort them from the compiler.

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 GesterX · May 21, 2011 at 09:31 PM 0
Share

You need to move the Destroy call to the end. Else the GO will be destroyed and the Instantiate won't be called.

avatar image
0

Answer by Luke Briggs · May 21, 2011 at 11:55 PM

P.s. The wierd code formatting of my last message isn't my fault, plus it won't let me edit it again without erroring :P

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 Luke Briggs · May 22, 2011 at 09:56 AM

If you'd like any explanation of how that's working please just let me know :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destory() on Collision if Tag = Lazer [SOLVED] 1 Answer

how do i make an Enemy take damage from prefab bullet clone? 1 Answer

Lower Object Health If Collides With "Player" Tag,Lower Object Health If Collides With Player Tag 1 Answer

Destroy object on touch of object with specific class 3 Answers

Edit - Code isolation using random ID numbers C# 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