Hi, I’m trying to achieve change of game objects and their children seamlessly, depending on the amount of clicks are achieved. In current build, you can click anywhere and the game and continue to next game object, just for testing purposes. Every second shape has weirdly working children, I used lists for this case. Could anyone please help me? Code and video attached.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class ShapeManager : MonoBehaviour
{
public List<GameObject> shape;
public int currentShape = 0;
public List<GameObject> points; // clickable points
public int clickCount = 0; // current amount of clicks
public GameObject RandomPoint; // random selected that is active
int[] shapeClicks = { 4, 8, 12, 16, 20 }; // amount of clicks it takes to move to next shape
void Start()
{
CreateShape();
}
void Update()
{
Click();
}
void Click()
{
if (Input.GetMouseButtonDown(0))
{
clickCount++;
// Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
// RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
Debug.LogWarning("Clicked " + clickCount + " times");
if (clickCount == shapeClicks[currentShape])
{
DeleteShape();
CreateShape();
}
else if(clickCount < shapeClicks[currentShape])
{
RandomPoint.SetActive(false);
GetRandomPoint();
}
}
}
void CreateShape() // adds new shape
{
Instantiate(shape[currentShape]);
FindAllPoints();
GetRandomPoint();
}
void DeleteShape() // clears old shape
{
Destroy(GameObject.FindWithTag("Shape"));
currentShape++;
}
void FindAllPoints() // finds all available points
{
points.Clear();
points.AddRange(GameObject.FindGameObjectsWithTag("Point"));
}
void GetRandomPoint() // generates random point from all available points
{
for (int i = 0; i < points.Count(); i++)
{
points[i].gameObject.SetActive(true);
}
int RandomInteger = UnityEngine.Random.Range(0, points.Count());
RandomPoint = points[RandomInteger];
for (int i = 0; i < points.Count(); i++)
{
if (points[i] == RandomPoint)
{
points[i].SetActive(true);
}
else
{
points[i].SetActive(false);
}
}
}
}