C# Deleting Game Objects from custom list object..

Ok guys, I put together some code and it works…

It created the cone object and stores it in my array…

adds it like this…

	Transform clone = Instantiate(_worldobjectmanager.PrefabBaseBlock, transform.position,transform.rotation) as Transform;
						clone.transform.position = transform.position + new Vector3 (i + _worldobjectmanager.MapOffSet,0, j + _worldobjectmanager.MapOffSet); 
						
					    clone.parent = _gameparent;
						
						MapBlock myblock = new MapBlock();
						myblock.transform = clone;

						_worldobjectmanager.MapBlocks.Add(myblock);

the list class object

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MapEditor
{
	public class MapBlock : IEquatable<MapEditor.MapBlock>{
		
		private Transform _transform;
		
		public Transform transform{
			get{return _transform; }
			set{_transform = transform; }
		}
		
		public bool Equals (MapBlock other)
		{
			throw new NotImplementedException ();
		}
			
		public enum BlockType : byte
		{
    	Ground= 0,
    	Stone = 1,
    	Water = 99
		}
			
	}
}

then i try to remove it like this…

	public void ClearTerrain(){
		foreach (MapBlock myblock in _worldobjectmanager.MapBlocks)
			{
				Destroy(myblock.transform.gameObject);
			}
		}

But it does not remove…

any one have any idea what I am doing wrong ?

What I notice is that you instantiate an object ‘clone’ then assign it back as the transofrm of myblock, so perhaps the Destroy call is destroying the reference and not the sibling transform of clone. Try using AddComponent( ) instead of assigning the transform back to clone.transform, also use GameObject.Destroy()/DestroyImmediate() and see if that helps.

Sorry but I am not sure i understand how unity uses AddComponent…
Any help on how I would apply it ?

Thanks! much appreciated.

public Transform transform{

            get{return _transform; }

            set{_transform = value; }

        }

You need to use ‘value’ instead of ‘transform’ in the set method of your property.

Thanks that was it.
Cant believe I missed that.

Also note to any one that trys to use the code.
The offset above is wrong for position.
Need to * to move the vector not +

Ohh and thanks everyone for the help.

This forum don’t work the best on my android.