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 mike1233 · Jan 29, 2017 at 10:41 PM · scripting problemdestroyspawninginstantiation

my destroy wall code keeps destroying entire object for good

this script does kill gameobject after contact but it kills even the future ones

I want it to kill the one that touches wall only not all I want it to keep instantiating them but it stops because this script kills it off entirely

using UnityEngine; using System.Collections;

public class wall5killfish : MonoBehaviour { //public Rigidbody2D projectile; //private Rigidbody2D projectileInstance = null;

     void Start()
     {
     }

     //void OnCollisionEnter2D(Collision2D other)
     void OnTriggerEnter2D(Collider2D other) 
     {

         if (other.gameObject.tag == "wall5") 
         {
         //Destroy(projectileInstance.gameObject);
             Destroy (gameObject);
             //if the bird collides with something set it to dead...
             //isDead = true;
             //GetComponent<AudioSource> ().Play ();
         }
     }
 }
         
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
0
Best Answer

Answer by dCalle · Jan 29, 2017 at 10:57 PM

the script kills itself....

because you forgot to put other. in front of gameObject

 Destroy(other.gameObject);
Comment
Add comment · Show 11 · 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 mike1233 · Jan 29, 2017 at 11:30 PM 0
Share

not working I put that on the gameobjects correct? either way it doesn't work

avatar image mike1233 · Jan 29, 2017 at 11:34 PM 0
Share

I dont want to destroy the wall I want to destroy the gameobects after they touch the wall

I think your "other" kills the wall

avatar image dCalle · Jan 29, 2017 at 11:37 PM 0
Share

alright then where are your other gameobjects created?

avatar image mike1233 dCalle · Jan 29, 2017 at 11:43 PM 0
Share

heres 1 of the scripts I have on them

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletFireScript : $$anonymous$$onoBehaviour {
 
     public float fireTime = .05f;
     public GameObject bullet;
 
     public int pooledAmount = 20;
     List< GameObject> bullets;
 
 
     void Start () {
 
         Debug.logger.logEnabled = false;
 
 
 
         bullets = new List <GameObject> ();
         for (int i = 0; i < pooledAmount; i++) {
             GameObject obj = (GameObject)Instantiate (bullet);
             obj.SetActive (false);
             bullets.Add (obj);
         }
         InvokeRepeating ("Fire", fireTime, fireTime);
     }
             void Fire()
     {
                 for (int  i  = 0; i < bullets.Count;  i++)
         {
             if (!bullets [i].activeInHierarchy) 
             {
                 bullets [i].transform.position = transform.position;
                 bullets [i].transform.rotation = transform.rotation;
                 bullets [i].SetActive (true);
                 break;
             }
         }
     }
 }
avatar image dCalle mike1233 · Jan 29, 2017 at 11:50 PM 0
Share

^^ it's because you've only created some bullets and the script iterates through them and reuses the ones that are DISABLED.

replace`Destroy(gameObject);`

with gameObject.SetActive(false);

Show more comments
Show more comments
avatar image mike1233 dCalle · Jan 30, 2017 at 12:04 AM 0
Share

dcalle I think you right

I removed the bullet with the pool script and it works with the other bullet prefabs that DOTN have the pool script why is this happening I thought pooling was good =---- it seems like pooling is a bunch of trouble so wen I changed the prefabs to the regular bullets with no pool code it works perfect

why is this happening? is pooling bad?

avatar image dCalle mike1233 · Jan 30, 2017 at 12:17 AM 0
Share

nope champ. pooling is good. because it prevents your CPU from doing more than he needs to do.

Try it again with the pool script and the gameObject.setActive(false);

how does your bullet fly? do you have a script here?

Show more comments
avatar image
0

Answer by mike1233 · Jan 30, 2017 at 12:25 AM

 public class ObjectPool : MonoBehaviour
     {
         public static ObjectPool current;            
         public GameObject[] prefabs;                
         public List<GameObject>[] pooledObjects;    
         public int[] amountToBuffer;                
         public int defaultBufferAmount = 10;        
         public bool canGrow = true;                
         GameObject containerObject;                        
         void Awake ()
         {
             if (current == null)
                 current = this;
             else
                 Destroy(gameObject);
             containerObject = new GameObject("ObjectPool");
             pooledObjects = new List<GameObject>[prefabs.Length];
             int index = 0;
                         foreach ( GameObject objectPrefab in prefabs )
             {
                 pooledObjects[index] = new List<GameObject>(); 
                                 int bufferAmount;
                 if(index < amountToBuffer.Length) 
                     bufferAmount = amountToBuffer[index];
                 else
                     bufferAmount = defaultBufferAmount;
                                 for ( int i = 0; i < bufferAmount; i++)
                 {    
                     GameObject obj = (GameObject)Instantiate(objectPrefab);
                     obj.name = objectPrefab.name;
                     PoolObject(obj);
                 }
                                 index++;
             }
         }
         public GameObject GetObject( GameObject objectType)
         {
                         for(int i=0; i<prefabs.Length; i++)
             {
                                 GameObject prefab = prefabs[i];
                 if(prefab.name == objectType.name)
                 {
                                         if(pooledObjects[i].Count > 0)
                     {
                         GameObject pooledObject = pooledObjects[i][0];
                                                 pooledObjects[i].RemoveAt(0);
                         pooledObject.transform.parent = null;
                         return pooledObject;    
                     }
                     else if(canGrow) 
                     {
                         Debug.Log("pool grew when requesting: " + objectType + ". consider expanding default size.");
   GameObject obj = Instantiate(prefabs[i]) as GameObject;
     
                         obj.name = prefabs[i].name;
                         return obj;
                     }
                     break;
                 }
             }
             return null;
         }
         public void PoolObject ( GameObject obj )
         {
                         for ( int i=0; i<prefabs.Length; i++)
             {
                 if(prefabs[i].name == obj.name)
                 {
                                         obj.SetActive(true);
                     obj.transform.parent = containerObject.transform;
                                         pooledObjects[i].Add(obj);
                     return;
                 }
             }
         }    
     }
     
Comment
Add comment · Show 3 · 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 dCalle · Jan 30, 2017 at 12:30 AM 0
Share

well you don't need that script on the bullet haha. Don't you have a script that makes the bullet fly around?

avatar image mike1233 dCalle · Jan 30, 2017 at 12:30 AM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletScript : $$anonymous$$onoBehaviour {
 
 
 
     public float speed = 1;
 
     // Update is called once per frame
     void Update ()
     {
         //transform.Translate (0, speed * Time.deltaTime, 0);
 
         transform.Translate (-speed * Time.deltaTime, 0, 0);
     }
 }
 
avatar image mike1233 dCalle · Jan 30, 2017 at 12:31 AM 0
Share

so its true that pool script was the problem correct?

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

112 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

Related Questions

Object Pooling Spawn Script 1 Answer

C# OnCollisionEnter not Triggering If Statement 1 Answer

I need help with my spawning system(SOLVED) 1 Answer

Checkpoint and Respawn 0 Answers

Buncha questions about scripts. 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