Answer the question
In order to leave comments, you need to log in
How to remove object from hierarchy using beam?
Good day inhabitants of the Toaster! For a long time there were no questions on the topic of Programming and Game Development, I think it's time to ask a question :)
I decided to finish the Build System while getting rid of some unnecessary scripts (Thus slightly optimizing the game itself). So I reached a dead end on the Destruction of objects inside the Parent! I wanted to remove an object from the hierarchy using the beam from the mouse cursor, but the method deleted the hierarchy itself, and after that it was possible to safely delete objects since they were no longer in the hierarchy ... Here is the script itself, or rather its parts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuildingSystem : MonoBehaviour
{
public LayerMask detailLayer;
public bool IsBuilding;
void Update()
{
if (IsBuilding && Input.GetMouseButtonDown(1))
{
Destroy();
}
}
public void Destroy()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 20, detailLayer))
{
Destroy(hit.transform.gameObject);
}
}
}
Answer the question
In order to leave comments, you need to log in
I didn't really understand what the question was. How to delete only a couple of children inside instead of the entire parent object??
There's a whole host of options here.
1) Separate layers / tags for the necessary objects (we also look where the beam hit, but not the first object that came across, but everyone whom the beam hooked. RaycastAll method)
2) still hitting the parent object with the beam - just search by name (if the same type or objects with a specific component: FindChild , GetComponent , etc.
3) Probably my favorite variation of option number 2 is to hang a script (or different scripts with inheritance) on parent objects of the same type, ala "Building info" which thread is BuildingComponentRefs.cs , in which there are a couple of public links with the necessary objects substituted. and perhaps even a couple of methods for working with them, to simplify your life. all sorts of ala "Turn off all" "Turn on all" "Mark red" and so on and so forth. Get prefabs with already configured / substituted links. and you don’t need to look for anything, assuming that this “describing” script hangs on the parent object (and since links are substituted with handles,
If the collider in which the rake hits is hanging on the parent, then everything is correct, it will remove the parent. Well, this code is quite difficult to build any assumptions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question