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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by superstar123 · Jun 25, 2011 at 02:26 PM · colliderprefabdestroyprefabsaccess

Change value one prefab NOT ALL

I spent half a day finding how to change value one prefab NOT ALL

This is my example:

First, I attach this script "Enemy_Health" to the enemy prefab:

 //My enemy's health is 2

static var HEALTH = 2;

Second, I tag my prefab enemy "enemy", then attach this script to my bullet:

 function OnTriggerEnter(hit : Collider) {
     if(hit.gameObject.tag=="enemy")
     {
 //get the script "Enemy_Health" and then subtract the health of enemy from the script
         var enemy : Enemy_Health = gameObject.GetComponent(Enemy_Health);
         enemy.HEALTH -=1;
 //Destroy only an enemy prefab collider, not all prefab
         if(enemy.HEALTH <=0)
         {
             Destroy(hit.gameObject);
         }
     }
 }

I don't know my script didn't work as my expectation( change value HEALTH one prefab and destroy that prefab NOT ALL ).

In this case, kill the enemy after shooting 2 times

On contrast, what my script did is when my bullet hit the obJect, other instantiating enemyprefabs are destroyed immediately and other left over enemyprefabs in my screen game are destroyed by only ONE buttlet, not 2.

Someone know this problem?

Thanks for your time, regards.

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
2
Best Answer

Answer by Joshua · Jun 25, 2011 at 02:31 PM

You have the variable HEALTH marked as static. Normal variables 'exist in their instance', meaning that if you have two enemies with the script

 var health : int;

then you can set the health of one of them to 50 and the health of the other to anything else.

Static variables however 'exist in the scene'. There is only one of them at all times. I don't know why you have HEALTH be a static variable in this case, but it does exactly what it does - if you change it in one instance of the script it changes for all.

I'm guessing the reason you did it here is so you can access it more easily? scriptName.theStaticVariable is how you access a static variable because for that scriptName there is only one version of theStaticVariable. If you want to access a normal variable you have to get it from that instance of the script using GetComponent.

[edit] here's what it should look like. The first script is called Enemy_Health, the second is called whatever you want.

 var health : int = 2;

that's the first, then the second:

 function OnTriggerEnter( hit : Collider )
 {
     if( hit.GameObject.tag == "Enemy" )
     {
         var enemyHealth : int = (hit.gameObject.GetComponent( Enemy_Health ) as Enemy_Health).health;
         enemyHealth--;
         if( enemyHealth <= 0 )
         {
             Destroy( hit.gameObject );
         }
     }
 }
Comment
Add comment · Show 8 · 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 superstar123 · Jun 25, 2011 at 03:14 PM 0
Share

I know and try to fix my script but it's no result. Because when I use your hint with this script: var enemy : Enemy_Health = gameObject.GetComponent(Enemy_Health); enemy.HEALTH -=1;

I get the report "HEALTH" is not an static member of "Enemy_Health"

On the other hand, would you know another script that destroy the enemy prefab after 2 hit? I know how to do this with 1 enemy, but with multiple enemy instantiating over time, I don't know :(

avatar image Joshua · Jun 25, 2011 at 03:23 PM 0
Share

Updated the answer with how I would do it. After going through your script again I spotted the error you say

  var enemy : Enemy_Health = gameObject.GetComponent(Enemy_Health);

but you want to access this script in the enemy, not on yourself. So make it hit.gameObject.GetComponent.....

avatar image superstar123 · Jun 25, 2011 at 04:00 PM 0
Share

Thank Joshua, That's better but not effective, I don't know why enemy can't be destroy, so I add this line after the line enemyHealth--; print(enemyHealth); and ops, the report is always "1" ? what happened here?

avatar image Joshua · Jun 25, 2011 at 04:03 PM 0
Share

You have to go out of the trigger and re-enter for it go decrement again

avatar image Bunny83 · Jun 25, 2011 at 05:15 PM 1
Share

An integer is a value type. When you assign it to a local variable only the value gets copied. If you change the local variable the original variable won't change.

 if( hit.GameObject.tag == "Enemy" )
 {
     var enemy : Enemy_Health;
     enemy = hit.gameObject.GetComponent(Enemy_Health);
     enemy.health--;
     if( enemy.health <= 0 )
     {
         Destroy( hit.gameObject );
     }
 }
Show more comments
avatar image
0

Answer by superstar123 · Jun 25, 2011 at 05:33 PM

Thanks a lot Bunny83. great code, tested and it works :) However thank Joshua for you time ;)

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

Destroyed prefab still moving 1 Answer

some prefab collisions randomly stop working 2 Answers

Object.Destroy destroying all prefabs 1 Answer

Destroy gameObject leaves behind collider? 1 Answer

Play animation before death...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