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 Martin 11 · Jun 12, 2012 at 04:30 PM · c#instantiateprefabfunctionmultiple

Why does this prefab trigger all instances to action? C#

I've a turret prefab with script (below) attached and a large collision volume to act as trigger. The prefab is instantiated in 2 locations in the world. However, when enemy enters the trigger volume of 1 of the instances they both fire.

I've looked around for similar answers and tried lots to fix myself but for the life of me I can't see why this is doing this.

Any help MUCHLY appreciated :) - you guys have helped me so much already through me reading your fixes to others' problems.

 public class GunController : MonoBehaviour
 {
 
     private float FireRate = 5.0f;
     private float TimeBank = 0f;
     private static int enemycount = 0;
 
     void Start()
     {
         TimeBank = Time.time;          
     }
 
     // Update is called once per frame
     void Update()
     {
         if (enemycount > 0)
         {
             if ((Time.time >= FireRate + TimeBank))
             {
                 SpawnBullet();
                 TimeBank = Time.time;
             }
         }
     }
 
     void SpawnBullet()
     {
         GameObject BasicProjectile = (GameObject)Resources.Load("BasicProjectile");
         Instantiate(BasicProjectile, transform.position, transform.rotation); // spawn BasicProjectile
     }
 
     void OnTriggerEnter(Collider other)
     {
         enemycount ++;
         Debug.Log(enemycount + " ENEMY IN");
     }
 
     void OnTriggerExit(Collider other)
     {
         enemycount --;
         Debug.Log(enemycount + " ENEMY OUT");
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Jun 12, 2012 at 04:36 PM

This happens because you are using a static variable to count the number of enemies that have entered the volume! This means that if one of them get triggered, all of them will count it, because the counter is shared between all of them!

You should just remove the 'static' keyword. It's not required, or even a good idea at the best of times.

What you are seeing here is why it's generally considered bad programming practice to use static variables in an object-oriented language. You should avoid them whenever you can.

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 Martin 11 · Jun 13, 2012 at 05:01 AM 0
Share

Yup. That was it. worked instantly.Thanks!

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

Multiple object prefab instantiates too far down 0 Answers

How to instantiate multiple prefabs in multiple positions? 0 Answers

How to instantiate multiple clones of the same prefab ? 1 Answer

How to randomly instantiate other prefabs parallel? 1 Answer

Destorying prefab and instanstiating again? 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