c# - How can i get a specific GameObjects to array without using FindGameObjectsWithTag? -
in first script clone gameobjects:
using system; using unityengine; using random = unityengine.random; using system.collections; using system.collections.generic; using system.linq; public class cloneobjects : monobehaviour { public gameobject objecttocreate; public int objectsheight = 3; [hideininspector] public gameobject[] objects; // tracking properties change private vector3 _extents; private int _objectcount; private float _objectsize; private list<gameobject> clonelist = new list<gameobject>(); /// <summary> /// how far place spheres randomly. /// </summary> public vector3 extents; /// <summary> /// how many spheres wanted. /// </summary> public int objectcount; public float objectsize; public static float largestsize = 0; // use initialization void start() { clone(); //objects = gameobject.findgameobjectswithtag("clonedobject"); objects = clonelist.toarray(); foreach (var element in objects) { float size = element.transform.localscale.x; if (size > largestsize) largestsize = size; } } private void onvalidate() { // prevent wrong values entered extents = new vector3(mathf.max(0.0f, extents.x), mathf.max(0.0f, extents.y), mathf.max(0.0f, extents.z)); objectcount = mathf.max(0, objectcount); objectsize = mathf.max(0.0f, objectsize); } private void reset() { extents = new vector3(250.0f, 20.0f, 250.0f); objectcount = 100; objectsize = 20.0f; } // update called once per frame void update() { } private void clone() { if (extents == _extents && objectcount == _objectcount && mathf.approximately(objectsize, _objectsize)) return; // cleanup //var objectstodestroy = gameobject.findgameobjectswithtag("clonedobject"); var objectstodestroy = objects; foreach (var t in objectstodestroy) { if (application.iseditor) { destroyimmediate(t); } else { destroy(t); } } var withtag = gameobject.findwithtag("terrain"); if (withtag == null) throw new invalidoperationexception("terrain not found"); (var = 0; < objectcount; i++) { var o = instantiate(objecttocreate); clonelist.add(o); o.transform.setparent(base.gameobject.transform); o.transform.localscale = new vector3(objectsize, objectsize, objectsize); // random position var x = random.range(-extents.x, extents.x); var y = extents.y; // sphere altitude relative terrain below var z = random.range(-extents.z, extents.z); // send ray down terrain adjust y according terrain below var height = 10000.0f; // should higher highest terrain altitude var origin = new vector3(x, height, z); var ray = new ray(origin, vector3.down); raycasthit hit; var maxdistance = 20000.0f; var nametolayer = layermask.getmask("terrain"); var layermask = 1 << nametolayer; if (physics.raycast(ray, out hit, maxdistance, layermask)) { var distance = hit.distance; y = height - distance + y; // adjust } else { debug.logwarning("terrain not hit, using default height !"); } // place ! o.transform.position = new vector3(x, y + objectsheight, z); } _extents = extents; _objectcount = objectcount; _objectsize = objectsize; } }
before in first time used give children objects clone tag name. do:
var objectstodestroy = gameobject.findgameobjectswithtag("clonedobject");
but i'm not using line. cloned objects untagged. i'm not using line.
but want cloned objects script:
void start() { anims = getcomponent<animations>(); waypoints = gameobject.findgameobjectswithtag("clonedobject"); originalposition = transform.position; }
but since cloned onjects without tag waypoints empty. waypoints array of gameobject: gameobject[] waypoints.
should give tag name every cloned object ? or there other way cloned objects array/list ?
solution a
make clonelist
public or create public property returns it, access script.
public class cloneobjects : monobehaviour { public list<gameobject> clonelist = new list<gameobject>(); } public class anotherscript : monobehaviour { void start() { var cloneobjects = obj_with_cloneobjects.getcomponent<cloneobjects>(); //cloneobjects.clonelist } }
solution b
create field of type list<gameobject>
in script. after cloning, assign clonelist
field.
public class cloneobjects : monobehaviour { public list<gameobject> clonelist = new list<gameobject>(); void clone() { } void start() { clone(); obj_with_anotherscript.getcomponent<anotherscript>.clonelist = clonelist; } } public class anotherscript : monobehaviour { public list<gameobject> clonelist = new list<gameobject>(); void start() { //this.clonelist } }
Comments
Post a Comment