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 /
This question was closed May 07, 2016 at 05:19 PM by meat5000 for the following reason:

Duplicate Question - Do a search

avatar image
0
Question by mohamedkhlifi · May 07, 2016 at 04:29 PM · instantiatearraydestroy

How to destroy an instantiated object when I touch it?

Hi, I want to destroy intantiated objects before they reach a given target. This is what ObjectPool.cs contains:

using UnityEngine;

using System.Collections; using UnityEngine.UI;

public class ObjectPool : MonoBehaviour

{

 GameObject[] projectiles = null;

 public int numberOfProjectilesToCreate = 1;

 public AudioSource audio1;



 // Use this for initialization
 void Start ()
 {

     projectiles = new GameObject[numberOfProjectilesToCreate];

     InvokeRepeating ("InstantiateProjectiles", 1f, 0.2f);


 }



 // Update is called once per frame
 void Update () {
     

     ActivateProjectile();
     DestroyByTouch ();

 }



 private void InstantiateProjectiles()
 {
     for (int i = 0; i < numberOfProjectilesToCreate; i++) 
     {
         projectiles [i] = Instantiate (Resources.Load ("Prefabs/Prefab_projectile")) as GameObject;

     }
 }





 private void ActivateProjectile()
 {
     for(int i=0; i<numberOfProjectilesToCreate;i++)
     {
         if (Mathf.Round(projectiles [i].transform.position.y) == 130) 
         {
             projectiles[i].SetActive(false);
         }

             projectiles[i].SetActive(true);
             projectiles[i].GetComponent<projectile>().Activate();


             return;

     }
 }



 private void DestroyByTouch()
 {
     Vector2 touchPos;
     
     for (int j = 0; j < numberOfProjectilesToCreate; j++) {
         for (var i = 0; i < Input.touchCount; ++i) {
             if (Input.GetTouch (i).phase == TouchPhase.Began) {

                 if (Input.GetTouch (i).tapCount > 0) {
                     touchPos = Input.touches[0].position;
                     Debug.Log (Mathf.Round (touchPos.x)+" , "+Mathf.Round (touchPos.y));

                 }
             }

         }
     }




 }

}

And this is what projectile.cs contains:

using UnityEngine; using System.Collections;

public class projectile : MonoBehaviour {

 public float life = 2f;
 private float timeToDie = 0.0f;
 public Vector3 targetPosition = new Vector3(0,-2.5f,0);
 public float speed = 12f;
 public float threshold = 0;




 // Update is called once per frame
 void Update () {

     CountDown ();
     AutoMove ();

 }





 public void Activate()
 {
     timeToDie = Time.time + life;
     transform.position = new Vector3 (Random.Range (-6.00f, 6.00f), 9f, 0);

 }






 private void Deactivate()
 {
     this.gameObject.SetActive(false);
 }


 private void CountDown()
 {
     if(timeToDie<Time.time)
     {
         Deactivate ();
     }
 }


 private void AutoMove()
 {
     Vector3 direction = targetPosition - transform.position;
     if(direction.magnitude > threshold){
         direction.Normalize();
         transform.position = transform.position + direction * speed * Time.deltaTime;
     }else{ 
         // Without this game object jumps around target and never settles
         transform.position = targetPosition;
     }
 }

}

Comment
Add comment · Show 1
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 meat5000 ♦ · May 07, 2016 at 05:19 PM 0
Share

Common Question

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

using destroy with an array of GameObjects (C#) 1 Answer

Parenting an instantiated prefab. 1 Answer

Destroying 2D Array of Instantated Objects 2 Answers

Destroying Instantiated Prefab 1 Answer

Which is faster for a pool of objects, instantiating and adding to an array or loading pre-made objects to an array? 1 Answer


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