Reputation: 37
Ok, I made preafab whic represent match. With script attached to main camera I made 15 matches,
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;
}
}
}
and I got this
When I click on the object object have to be destroyed (made that) but when I click on the object in some row I have to made that only that object can be destroyed all others matcher in other row have to became undestroyable/unclickabe till button is pressed.
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 made some object undestroyable/unclikable in script.
Upvotes: 1
Views: 219
Reputation: 37
Ok, I made solution it is very simple.
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;
}
}
In function select matches I check a GameObject array in whic are Matces/Playing object and check the number/name of object. I knew postion of object and on that I change name only object whic are in the range. I made global static bool variable whic is break block changing a tag till I press a button. Because of array I don't use destroy object, SetActvie tru is better because this aproach don't make mess with array.
Upvotes: 1
Reputation: 1648
By doing this . You all just need to do is tag the gameobject as DontDestroy And it's undestroyable
object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
foreach (object o in obj) {
GameObject go = (GameObject) o;
//if the GO is tagged with DontDestroy, ignore it. (Cameras, Managers, etc. which should survive loading)
//these kind of GO's shouldn't have an ObjectIdentifier component!
if(go.CompareTag("DontDestroy")) {
Debug.Log("Keeping GameObject in the scene: " + go.name);
continue;
}
Destroy(go);
}
Hope i helped
Upvotes: 2
Reputation: 212
You can set the UI to not take in input at the certain spot of an object, basically say the cursor isn't over the object so you can't click. That's the closest thing I can find/think of for helping that.
public class GraphicIgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
{
public bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
return false;
}
}
More information about ^ that concept/script: http://answers.unity3d.com/questions/816861/46-ui-image-is-capturing-clicks-how-to-prevent.html
As for setting an object to be unable to be destroyed, that's just not something Unity does.
Upvotes: 0