Search Unity

Destroying GameObjects With Different Names?

Discussion in 'Scripting' started by Endzone, Apr 27, 2015.

  1. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Hello, just wondering if some1 could help me im current developing a voxel based game and im in the middle of creating lanterns, an so what im trying to do is once i have placed my block im instantiating a Light and then renaming it such as Light#0, Light#1, Light#2 and so on and this brings me to my question im having trouble with after its been renamed so its no longer Light(Clone) once i destroy the block i want the light to be destroyed also some how but im not exactly sure how to go about Getting the exact Light as you can probably tell, i did try useing RayCasts but they just hit the block and not the light source... .... :l - any help is greatly appreshiated & sorry if my code is a little all over the place im semi new to C#..
    P.S: Dont think my code below is Functional it will most likely have errors i left it As is just so if anyone can help they have a better understanding of Exactly what my issues is and kinda what i want my outcome to be just incase i didnt explain it well enough.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TorchID : MonoBehaviour {
    5.  
    6.     private int i;
    7.     public Transform RedLight;
    8.  
    9.     public void OnTorchPlace ()
    10.     {
    11.         Debug.Log ("Lantern has been Placed");
    12.             var t = GameObject.Find ("RedLight(Clone)");
    13.             t.name = "LightSource#" + i;
    14.             i++;
    15.     }
    16.  
    17.     public void torchDestroyed()
    18.     {
    19.         Debug.Log ("Lantern has been Destroyed");
    20.         Destroy (GameObject.Find ( transform.LookAt(RedLight)));
    21.     }
    22. }
     
    Last edited: Apr 27, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how are you instantiating the torches? if they are a child of the block you could give the block an "OnDestroy()" function which destroys any children. Then it doesn't matter what they are called, the block knows what transforms are it's children and destroys them.
     
  3. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Hey thanks for Replying - That would work although the Block and the lights unfortunately are different objects and arnt children of the Torches because the way the voxel engine is build on blocks being placed they dont show in the hierarchy im not exactly 100% on how it all works but the lights arnt anything to do with the torches they are a seperate Object that im instantiating within the block once it has been place
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    oh, you're using a third party voxel engine? which one?
     
  5. Endzone

    Endzone

    Joined:
    Dec 18, 2014
    Posts:
    119
    Uniblocks :) its been pretty good so far would be evan better if i could figure out how to fix my problem tho haha its been bugging me for about a week only just got the ergy to be botherd to make a thread :p