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 /
  • Help Room /
avatar image
0
Question by UAdrian · Jun 09, 2016 at 05:27 PM · c#unity 5unity5prefabs

Spawned prefabs are sharing variables values

I'm trying to make a FPS with zombies but i have a problem when i try to decrease zombie health. I attached to zombie prefab this script:

 using UnityEngine;
 using System.Collections;
 
 public class ZombieHealth : MonoBehaviour 
 {
     private int health;
     private bool isDead;
 
     private Animator animator;
     private int count;
 
     void Awake () 
     {
         animator = GetComponent<Animator> ();
         health = Random.Range (75, 125);
     }
     
     IEnumerator OnTriggerEnter (Collider collider)
     {
         if (collider.tag == "Bullet" && isDead == false)
         {
             Destroy (collider.gameObject);
             health -= 10;
             count++;
             Debug.Log ("Health: " + health + "; Fire no:" + count);
 
 
             if (health <= 0) 
             {
                 isDead = true;
                 animator.SetTrigger ("dead");
 
                 yield return new WaitForSeconds (5);
 
                 Destroy (this.gameObject);
             }
         }
 
     }
 }

I'm spawnig zombies at an interval adn first one has helth for example 102 (sometimes die when it has 2 health, idk why) an if i let firs zombie with 42 health and shoot another zombie 4 or 5 times that one dies and after that every spawned zombie die at first shot and it's also playing dying animation if i shoot in him even if he is dead and isDead variable is true.

What can I do to correct the script?

P.S. I start unity 2 week ago so i'm still kind of noob.

EDIT: in console is showing the correct values

Spawn code:

 using UnityEngine;
 using System.Collections;
 
 public class ZombieSpawner : MonoBehaviour 
 {
     public GameObject[] zombie;
     public float spawnTime = 3f;
     public Transform[] spawnPoint;
 
     public static bool spawn;
 
     void Start () 
     {
         InvokeRepeating ("Spawn", spawnTime, spawnTime);
     }
 
     void Spawn ()
     {
         if (spawn) 
         {
             int zombieIndex = Random.Range (0, zombie.Length);
             int spawnIndex = Random.Range (0, spawnPoint.Length);
 
             Instantiate (zombie[zombieIndex], spawnPoint [spawnIndex].position, spawnPoint [spawnIndex].rotation);
         }
 
     }
 }
 

Comment
Add comment · Show 2
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 EpiFouloux · Jun 10, 2016 at 07:40 AM 0
Share

Can you show us the spawning part of the code?

avatar image UAdrian EpiFouloux · Jun 10, 2016 at 02:16 PM 0
Share

I updated the post

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jun 09, 2016 at 05:55 PM

Are you instantiating your zombies from a prefab, or are you instantiating them from the reference of the first zombie?

If it's the latter, then they will always copy the values at time of instantiation.

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 UAdrian · Jun 09, 2016 at 05:58 PM 0
Share

From a prefab

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

185 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Accessing GameObjects when changing scenes (levels) 1 Answer

Make GameObject “attach” properly? 0 Answers

ClientRpc Function not being carried out on clients in Unity3d c# 0 Answers

Properly attach to a GameObject after collision? 0 Answers

Varialbes on prefab script always null 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