This has been driving me nuts for a month. Here is the situation. I have 10 different colored objects on the screen all with a “sp” tag. I want to randomly choose one and make a copy of it on the screen. Only once for each object. all this happens on a collision.
function OnTriggerEnter(thing: Collider){ //
onelistWhites.clear();
listWhites = null;
nodesFixedArray=null;
nodesFixedArray = GameObject.FindGameObjectsWithTag("sp");
listWhites = new Array(nodesFixedArray);
i = Random.Range(0,listWhites.length);
var theObject : GameObject = listWhites*;*
*var newobject = Instantiate (theObject) as GameObject;;*
*listWhites.RemoveAt(i);*
*
*
on collision I create a random object from the array. remove it from the array. but sometimes the next time i want to create a random object from the array, one of the old ones are still there and it instantiates it again. It seems to me that the listWhites.RemoveAt(i); does not always work. or does not have time to work. is a batch of code in a function initiated one line at a time, or one the function reaches its end does it all fire at once?
The problem is that you’re recreating nodesFixedArray and listWhites each collision, so deleting elements has no effect at all. You should create nodesFixedArray and copy it to listWhites at Start (or other convenient function), and place the rest of your code in OnTriggerEnter:
var onelistWhites: Array;
function Start(){
var spObjects: GameObject[];
spObjects = GameObject.FindGameObjectsWithTag("sp");
onelistWhites = new Array(spObjects); // copy spObjects to onelistWhites
}
function OnTriggerEnter(thing: Collider){
var i = Random.Range(0,listWhites.length);
var theObject : GameObject = listWhites*;*
*var newobject: GameObject = Instantiate(theObject);*
*listWhites.RemoveAt(i); // eliminate the object from the list*
*...*
*}*
*
*
But you have other problems: 1- Instantiate(theObject) just clones the original object at its original position, so you will have both occupying the same space - you should specify the position and rotation of the clone to avoid this; 2- You must check the case when listWhites is completely duplicated and thus have no more elements.
hi i got a similar problem here.i m doing a puzzle game and i need to drag a clone object which was randomly array at the bottom of the scene.
this is the script for drag and drop that i did
static var clickedObj : Transform;
private var offSet : Vector3;
var cubewall : GameObject;
private var snapAllowance: int = 5;
var linkToArray : Array;
function Update () {
var ray = camera.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0)) {
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, Mathf.Infinity)) {
if(hit.collider.gameObject.tag == "Pipe") {
clickedObj = hit.transform;
offSet = clickedObj.position-ray.origin;
}
}
}
and this is the code for array:
var blockTypes: GameObject[];
var inGamePos: Array = new Vector3[4];
static var clickedObj : Transform;
var pos1: GameObject;
var pos2: GameObject;
var pos3: GameObject;
var pos4: GameObject;
function Start () {
inGamePos[0] = pos1.transform.position;
inGamePos[1] = pos2.transform.position;
inGamePos[2] = pos3.transform.position;
inGamePos[3] = pos4.transform.position;
var blockTypesWeight= [2,2,1,1,1,2];
for (i = inGamePos.length - 1; i >= 0; i--) {
var ranPipe: GameObject = blockTypes[Random.Range(0,blockTypes.length)];
var newPipe: GameObject = Instantiate(ranPipe,inGamePos*,Quaternion.identity);*
}*
} i have tried but i couldnt click and drag the pipe that is random arrayed at the bottom even though when i put the pipe to the “Cubewall”, i click and drag around perfectly fine. what i need to do to click and drag the pipe(clone) randomly arrayed at the bottom??