2
\$\begingroup\$

I have a prefab which is instantiated in the Scene and what I am trying to do is to disable with a touch or timeCounter but sometimes it disables all the instances of the prefab and sometimes 2 or 3 of them.

I want to disable particular object which I have touched currently.

when i am passing the collider in Destroy(hit.collider) it works perfect but how to handle it null is passed as parameter.

Here is my code:

 void Update () {
     blastingTimeCounter -= Time.deltaTime;

     if (blastingTimeCounter <= 0) {
         DestroySquare (null);
     }

     for (int i = 0; i < Input.touchCount; i++) {
         if (Input.GetTouch (i).phase == TouchPhase.Stationary) {
             hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (
                 Input.GetTouch (i).position), 
                 Vector2.zero);
             if (hit.collider != null) {
                         DestroySquare (hit.collider);
             }
         }
     }
  }

  void DestroySquare(Collider2D collider){

     if(collider == null)
     {

     }
     else{
     collider.gameObject.SetActive (false);
     blastingTimeCounter = blastingTime;
     }
   }
\$\endgroup\$
0
\$\begingroup\$

I think what you're trying to do is something like this:

void DestroySquare() {

    if (hit != null) {
        hit.transform.gameObject.SetActive(false);
    } else {} // Do nothing
}
\$\endgroup\$
  • \$\begingroup\$ this works perfect but when 'hit' != null , but at some condition i also want to disable it when hit == null, then what to do in that case? \$\endgroup\$ – user3191102 Apr 12 '16 at 14:20
  • \$\begingroup\$ when i am doing gameObject.setActive(false) in else statement as you have mentioned, it disables all the game object in the scene. \$\endgroup\$ – user3191102 Apr 12 '16 at 14:23
  • \$\begingroup\$ @user3191102 Are you sure you're doing hit.transform.gameObject? \$\endgroup\$ – EnragedTanker Apr 12 '16 at 14:28
  • \$\begingroup\$ yes it works but i am asking how to do the else part when hit is null? \$\endgroup\$ – user3191102 Apr 12 '16 at 14:43
  • 2
    \$\begingroup\$ A better option, @user3191102, would be to write a separate function that handles the time-up scenario when you do want to disable the objects but don't have a raycast hit. \$\endgroup\$ – Draco18s Apr 12 '16 at 16:36
0
\$\begingroup\$

Finally find out the solution and it works..

 void DestroySquare(Collider2D collider){

 if(collider == null)
 {
      GameObject obj = (GameObject)gameObject;
        obj.SetActive (false);



      //blastingTimeCounter = blastingTime;  this was changing in all the instances of a prefab 

    obj.GetComponent <BlackSquare2>().blastingTimeCounter = blastingTime; // correct




 }
 else{
 collider.gameObject.SetActive (false);
 //blastingTimeCounter = blastingTime;  this was changing in all the instances of a prefab

  collider.gameObject.GetComponent <BlackSquare2>().blastingTimeCounter = blastingTime; // correct


       }
}

I Just want to know why blastingTimeCounter = blastingTime is changing in all the instances of a prefab?

while doing this collider.gameObject.GetComponent <BlackSquare2>().blastingTimeCounter = blastingTime; it will reset blastingTimeCounter correctly.

\$\endgroup\$
  • \$\begingroup\$ because when you write blastingTimecounter it means it happens on all the Monobehaviours and when you do collider.gameObject.GetComponent<>, GetComponent returns the instance of the class that is running on this specific game object. \$\endgroup\$ – Uri Popov Apr 14 '16 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.