- Home /
Question by
fuadshahmuradov · Apr 25, 2020 at 07:30 PM ·
2dgameobjecttiled
Unity 2d remove gameobject child at specific position
I am making game inspired by Plants vs Zombies. I spawn defenders when I click on the certain tile using DefenderSpawner.cs
. When defenders are instantiated they are childed at Defenders
parent. Now I want to add a shovel button, to remove certain defender at mouseclicked position.
This is the script for spawning. What can I do?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefenderSpawner : MonoBehaviour
{
Defender defender;
GameObject defenderParent;
const string DEFENDER_PARENT_NAME = "Defenders";
private void Start()
{
CreateDefenderParent();
}
private void CreateDefenderParent()
{
defenderParent = GameObject.Find(DEFENDER_PARENT_NAME);
if(!defenderParent)
{
defenderParent = new GameObject(DEFENDER_PARENT_NAME);
}
}
private void OnMouseDown()
{
if (defender != null)
{
AttemptToPlaceDefenderAt(GetSquareClicked());
}
else
{
return;
}
}
public void SetSelectedDefender(Defender defenderToSelect)
{
defender = defenderToSelect;
}
private void AttemptToPlaceDefenderAt(Vector2 gridPos)
{
var StarDisplay = FindObjectOfType<StarDisplay>();
int defenderCost = defender.GetStarCost();
if(StarDisplay.HaveEnoughStars(defenderCost) && !FindObjectOfType<PauseMenu>().GameIsPaused)
{
SpawnDefender(gridPos);
StarDisplay.SpendStars(defenderCost);
}
}
public Vector2 GetSquareClicked()
{
Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
Vector2 gridPos = SnapToGrid(worldPos);
return gridPos;
}
private Vector2 SnapToGrid(Vector2 rawWorldPos)
{
float newX = Mathf.RoundToInt(rawWorldPos.x);
float newY = Mathf.RoundToInt(rawWorldPos.y);
return new Vector2(newX, newY);
}
private void SpawnDefender(Vector2 roundedPos)
{
Defender newDefender = Instantiate(defender, roundedPos, Quaternion.identity) as Defender;
newDefender.transform.parent = defenderParent.transform;
}
}
Comment
Best Answer
Answer by fuadshahmuradov · Apr 25, 2020 at 08:55 PM
I found solution.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefenderRemover : MonoBehaviour
{
Camera mainCam;
bool removerSelected=false;
void Awake()
{
removerSelected = false;
mainCam = Camera.main;
}
private void OnMouseDown()
{
removerSelected = true;
}
void Update()
{
if(removerSelected)
{
GetComponent<SpriteRenderer>().color = Color.gray;
}
if (!removerSelected)
{
GetComponent<SpriteRenderer>().color = Color.white;
}
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.GetRayIntersection(mainCam.ScreenPointToRay(Input.mousePosition));
if (hit.collider != null && removerSelected && hit.transform.gameObject.tag=="removable")
{
Destroy(hit.transform.gameObject);
removerSelected = false;
}
}
}
}
Answer by qobion · Apr 25, 2020 at 07:51 PM
void RemoveDefender (Defender defender)
{
Destroy(defenderParent.transform.GetChild(defender.transform.GetSiblingIndex()).gameObject);
}