Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

RaycastHit2D - SetActive(false) to several objects

Discussion in 'Scripting' started by do4it, Jan 3, 2017.

  1. do4it

    do4it

    Joined:
    Nov 21, 2016
    Posts:
    2
    Hi, I have a problem. I'm able to disable (SetActive(false)) an object by touch and automatically turn it on after time. But when I touch several objects automatically turns on only one of them, the rest are disabled.

    Please help
    I use this script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchDelete : MonoBehaviour
    5. {
    6.     GameObject touchedObject;
    7.  
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    16.         {
    17.             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    18.             RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
    19.        
    20.             if (hit.collider != null && hit.collider.gameObject.tag == "delete")
    21.  
    22.             {
    23.                 touchedObject = hit.transform.gameObject;
    24.                 touchedObject.SetActive(false);
    25.                 Invoke("Active", 2f);
    26.             }
    27.         }
    28.     }
    29.  
    30.     void Active()
    31.     {
    32.         touchedObject.SetActive(true);
    33.  
    34.     }
    35. }
    36.  
     
    Last edited: Jan 3, 2017
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    You need to change your GameObject variable into a List add the hit gameObjects to it and then pass it to Active and only activate that
     
  3. do4it

    do4it

    Joined:
    Nov 21, 2016
    Posts:
    2
    Can I please you for changed code?
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    No you can't, this is not a forum where people do stuff for you, especially not, if the changes you ask me to do almost completely change the code you posted, please go ahead and do what you think might work, then if you got stuck after a lot of trying come back and post the code you changed
     
unityunity