• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Can't set the random position for an Instance. 1 Answer

The prefab you want to instantiate is null. 2 Answers

Issues with instantiated prefabs 1 Answer

Issue Instantiating prefab in C# 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges