Q: How to make a object which are made from script undestroyable

Ok, I have a question and I will try to explain what is my problem.

First I made 15 object from prefab, script is atached to Main Camera object

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class LoadMatches : MonoBehaviour {
    public GameObject match;
 
    GameObject[] matchArray = new GameObject[15];

    void Start () {
      
        DrawMatches();
     }
    private void DrawMatches()
    {
     
 
        int y = 4;
        int x = -4;
        int n = 0;
       
            for (int i = -4; i < 5; i += 2)
            {

                for (int j = x; j < y + 1; j += 2)
                {
              
                 matchArray[n]=Instantiate(match, new Vector3(j, i, 0), Quaternion.identity) as GameObject;
                 matchArray[n].name= Convert.ToString(n);
        
                 n++;
                
                }
                y = y - 1;
                x = x + 1;
            }
       
    }
 

}

I got this

15 matches in piradimal shape, and I need when to click on one of them to destrojy them. But I need to click on the object in row that only that row canbe active. That mean that I can destroy only object in that row and no other till press a button.

After that I have another script which is attached to prefab and that script controll clik on object

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class DragDrop : MonoBehaviour {
       float x;
    float y;
    public GameObject match;
    private txtGameOver txtGO;

    private void Awake()
    {
        txtGO = GameObject.FindObjectOfType<txtGameOver>();
    }

    private void Start()
    {
        match.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
      
     }
     void Update(){
        x = Input.mousePosition.x;
        y = Input.mousePosition.y;
    }
    void OnMouseDrag(){
        transform.position = Camera.main.ScreenToWorldPoint(new Vector3(x,y,10.0f));
    }
    void OnMouseDown()
    {
        var boxes = GameObject.FindGameObjectsWithTag("Match");
     
        SelectMatches(match);
        if (boxes.Length == 1)
        {
            txtGO.GameOverText();
        }
    }
    void SelectMatches (GameObject m)
    {
       int n;
       n = Convert.ToInt32(m.name);
         if (n>=0 && n<=4)
        {
          
            Destroy(m);
         }
      else if (n > 4 && n <= 8)
        {
             Destroy(m);
         }
        else if (n > 8 && n <= 11)
        {
            Destroy(m);
        }
        else if (n > 11 && n <= 13)
        {
             Destroy(m);
        }
        else if (n==14)
        {
            Destroy(m);
         }
    }
}

My Question is, How to make object undestroyable or unclicable in this specific situation.

I cannot display the code for the solution but I can give you a lead on how to get to your solution.

make several Cubes that will fill the play area. Set them in a way that they have little to no visibility. The player will have to select a row and then remove the match or matches. For the cubes you will make then like a highlighter and you will change the tag from not active to active, and you will then set the matches to react based on if the cube is active. If the cube is active each mache in that row can be destroyed.

Does that help?

1 Like

Yes it good solution but I solved a problem. I did eveeything wiht changing a nema of object. All object tag name Match when I click on one object all object in range change the name tag in “Destroy” and after that with if I checked the name and change objcet.setactive to false. I don’t use destroy function because was some problem with array.

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.Collections.Generic;

public class DragDrop : MonoBehaviour {
       float x;
    float y;
    public GameObject match;
    private txtGameOver txtGO;
     public GameObject[] GameObjectArray;

    private void Awake()
    {
        txtGO = GameObject.FindObjectOfType<txtGameOver>();
    }

    private void Start()
    {
      GameObjectArray = GameObject.FindGameObjectsWithTag("Match");
    }
     void Update(){
        x = Input.mousePosition.x;
        y = Input.mousePosition.y;
    }
void OnMouseDrag(){
    //    transform.position = Camera.main.ScreenToWorldPoint(new Vector3(x,y,10.0f));
    }
    void OnMouseDown()
    {
        var boxes = GameObject.FindGameObjectsWithTag("Match");
      
        SelectMatches(match);
        if (boxes.Length == 1)
        {
            txtGO.GameOverText();
        }
    }
    void SelectMatches (GameObject m)
    {
       int n;
       n = Convert.ToInt32(m.name);
       if (n>=0 && n<=4)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control==false)
            {
                ChangeTag(0, 4,n);
               
            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }

        }
      else if (n > 4 && n <= 8)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(5, 8, n);

            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
        else if (n > 8 && n <= 11)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(9, 11,n);

            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
        else if (n > 11 && n <= 13)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(12, 13, n);

            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
        else if (n==14)
        {
            if (GameObjectArray[n].CompareTag("Match") && GlobaVariables.Control == false)
            {
                ChangeTag(14, 14, n);

            }
            else if (GameObjectArray[n].CompareTag("Destroy") && GlobaVariables.Control == true)
            {
                GameObjectArray[n].SetActive(false);
            }
        }
   }
    void ChangeTag(int p, int z,int n)
    {
      
        for (int i = p; i <z+1; i++)
        {

          GameObjectArray[i].tag = "Destroy";
            Debug.Log(GameObjectArray[i].tag + " " + GameObjectArray[i]);
     

        }
     
        GameObjectArray[n].SetActive(false);
        GlobaVariables.Control = true;

    }
}