- Home /
need Block Placer/Eraser to not destroy any other gameobjects
As I am a noob to Unity I need some help. I tried to make a game with block placing/erasing and used this script:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build() {
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
}
}
function Erase() {
if (HitBlock())
Destroy(hit.transform.gameObject);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
The problem is that the script is also destroying my terrain and any other gameobject. I want to somehow detect if the object that is hit by the ray is a cube that is allowed to be destroyed or a object that shouldn't be destroyed.
Answer by GameVortex · Nov 20, 2013 at 10:23 PM
There are many ways of achieving this. One of the simplest ways that does not require extra scripts could be to just name the object something specific when you create it. That way you can check if the gameObect the raycast hit has the same name or not and then destroy it if it has.
Example:
function Build() {
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
cube.name = "ErasableCube";
}
}
function Erase() {
if (HitBlock())
if(hit.name == "EraseableCube") {
Destroy(hit.transform.gameObject);
}
}
You could also do this using the "Tag" property of gameObjects: Tag. You will need to create the tags in the tagmanager first though.
But there seems to be a problem when I want to right-click I get this error:
$$anonymous$$issingFieldException: UnityEngine.RaycastHit.name Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.FindExtension (IEnumerable`1 candidates) Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.Create (SetOrGet gos) Boo.Lang.Runtime.DynamicDispatching.PropertyDispatcherFactory.CreateGetter () Boo.Lang.Runtime.RuntimeServices.DoCreatePropGetDispatcher (System.Object target, System.Type type, System.String name) Boo.Lang.Runtime.RuntimeServices.CreatePropGetDispatcher (System.Object target, System.String name) Boo.Lang.Runtime.RuntimeServices+c_AnonStorey16.<>m_A () Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.Dispatcher$$anonymous$$ey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name)
would be nice if could get help again.
Aah, sorry about that. $$anonymous$$y bad, it should be:
If(hit.transform.name == "EraseableCube")