- Home /
How to activate hidden object which in game area in terms of cubes which are dragged and dropped?
Hi everyone. I try to make a game with cubes. One of my cubes is different from others in terms of scale. In the game, the user should drag and drop the cube to the game area. In the game area, I hid some objects to control whether the user dragged objects to the correct place or not. Even if their matchings correct or wrong, cubes are held in the area. However, when the user drags the different cube to the wrong place, the area is holding the cube like a square not like a triangle, so I have to organize hidden objects in terms of the different cube. To do that, I wrote some codes. It is finding the nearest object and then activated its answer set, but this code works only for the different object. I have to write some codes for others, too. How can I do that in a short, different, or easy way? Moreover, I added the script to different cubes but it works the others, too, and, I turned the cubes the script works again. I do not want to do that. How I should fix these problems?
public class activateHiddens: MonoBehaviour
{
public GameObject[] hiddenObjects;
public GameObject[] Sets;
void Start()
{
for (int setsIt = 0; setsIt < Sets.Length; setsIt++)
{
Sets[setsIt].gameObject.SetActive(false);
}
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
FindClosestArea();
}
}
public void FindClosestArea()
{
switch (gameObject.name)
{
case "longestCube":
float distanceToClosestArea = Mathf.Infinity;
GameObject closestArea = null;
GameObject[] allArea = GameObject.FindGameObjectsWithTag("hiddenAreas");
foreach (GameObject currentArea in allArea)
{
float distanceToArea = (currentArea.transform.position - kup.transform.position).sqrMagnitude;
if (distanceToArea < distanceToClosestArea)
{
distanceToClosestArea = distanceToArea;
closestArea = currentArea;
switch (closestArea.name)
{
case "hiddenUp":
Sets[0].gameObject.SetActive(true);
for (int setsIt = 1; setsIt < Sets.Length; setsIt++)
{
Sets[setsIt].gameObject.SetActive(false);
}
Debug.Log(Sets[0] + " activated.");
break;
case "hiddenLeft":
Sets[0].gameObject.SetActive(false);
Sets[1].gameObject.SetActive(true);
for (int setsIt = 2; setsIt < Sets.Length; setsIt++)
{
Sets[setsIt].gameObject.SetActive(false);
}
Debug.Log(Sets[1] + " activated");
break;
case "hiddenRight":
Sets[3].gameObject.SetActive(false);
Sets[2].gameObject.SetActive(true);
for (int setsIt = 0; setsIt < 2; setsIt++)
{
Sets[setsIt].gameObject.SetActive(false);
}
Debug.Log(Sets[1] + " activated");
break;
case "hiddenDown":
Sets[3].gameObject.SetActive(true);
for (int setsIt = 0; setsIt < 3; setsIt++)
{
Sets[setsIt].gameObject.SetActive(false);
}
Debug.Log(Sets[1] + " activated");
break;
}
}
}
break;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613020429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to create a Drag & Drop system which verifies what gameObject was dragged into the slot? 0 Answers
drag and drop in specific area Unity 3 Answers
3D Drag&Drop with collision 0 Answers