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 Dann858 · Jan 23, 2013 at 09:59 PM · c#updatetutorial

Getting Cube(Clone)(Clone)(Clone)(etc.) how to fix?

I've tried to create a 'dead body' of my enemy cubie. (I'm using cubes to finish scripting first, later I'll add models with animations etc., but I'm having one problem with it.)

I had to put my C# funtion:

 public void OnEnemyKilled() {
    if(curHealth == 0) {
    PrefabEnemy = Instantiate(PrefabEnemy) as GameObject;
    this.enabled = false;
    enemyAI.enabled = false;
    enemyAttack.enabled = false;
    }
 }

Into the void Update() function. To constantly check the curHealth. But because I've putted it into the Update() function, it now creates the PrefabEnemy over and over again. How do I fix this? I need to disable the funtion once it has been run? And how do I do this?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jan 23, 2013 at 11:07 PM

You should not assign anything to PrefabEnemy at runtime! The way you're doing this will append "(Clone)" to each new enemy, making incredibly long names!
In order to create a "dead body" to replace a dead enemy, you should have a different prefab, not reproduce the live enemy.
Declare a variable deadBody outside any function. When curHealth is zero and deadBody is null, create the dead body and assign its GameObject reference to deadBody - this way only the if code will be executed only once.

 public deadBodyPrefab: GameObject; // drag the dead body prefab here
 GameObject deadBody = null; // add this variable outside any function
 
 public void OnEnemyKilled() {
    if(curHealth == 0 && !deadBody){ // only create the dead body once
        deadBody = Instantiate(deadBodyPrefab) as GameObject;
        this.enabled = false;
        enemyAI.enabled = false;
        enemyAttack.enabled = false;
    }
 }
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 Staross · Jan 23, 2013 at 10:55 PM

Maybe:

 if(this.enabled && curHealth == 0)

So it's executed only once?

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 Pysassin · Jan 23, 2013 at 10:07 PM

At the end of this if statement make curHealth = -1. That way any functions that really on this being a positive number aren't negatively affected and since it no longer equals 0 it wont keep running the loop. Another way is to make an if statement inside of that statement that checks if the variable PrefabEnemy is created or not by simply doing...

if(PrefabEnemy)

either way should work for you.

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 Dann858 · Jan 23, 2013 at 10:20 PM 0
Share

Well, I've another script that defines the EnemyHealth itself. And in this script is stated that curHealth can't go below 0. So making it equal -1 wouldn't work at all.

If I have created this 'PrefabEnemy', how would it stop the Update function? I can't run the code if I don't have created it yet, but while stating it what has to follow the:

 if(PrefabEnemy) {
    //insert right code here
 }

?

avatar image Pysassin · Jan 23, 2013 at 11:05 PM 0
Share

As Sparks said in his answer. Checking for a variable in an if statement checks if the variable is null or not. The code you provide you Instantiate to the variable name PrefabEnemy, so I assume it is empty until then. Using if(PrefabEnemy) means IF there is something there. If you want to check the false place a ! infront of it like so if(!PrefabEnemy)

avatar image
0

Answer by sparkzbarca · Jan 23, 2013 at 10:42 PM

 if(enemyHP == 0 && NoPrefabYet)
 {
 instantiate(prefab);
 NoPrefabYet = false;
 }


what he is saying with the if(prefab) is basically you add another check.

The first check is to make sure the enemy is dead, the second check is to see if you've spawned the prefab to take care of that yet.

you start out with that equal to true, you dont have a prefab. Or if you use the existence of the prefab itself you do a if (!prefab) where you check for the NOT. A if false really.

then only when the enemy is dead AND things you do when enemy is dead hasn't happened yet, do you do the things you do.

Once you do those things like spawning the prefab something should change so that the next loop the if statement catches the change and doesnt run again.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

learn c#? please help. 1 Answer

Saving data. 1 Answer

Survival Shooter Tutorial fix for Unity 5 3 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