- Home /
Instantiate problem with multiple objects from an array
hello friends , i am trying to do some targeting GUI using NGUI but ran into some problem , here is it:
1:- i have a topdown shooter , the player gets a targeting brackets for near enemy's when near 100 units.
2:- i did it right with the normal GUI from unity and here is the script:
function OnGUI() {
lastPosition = Camera.main.WorldToScreenPoint(hitColliders[0].gameObject.transform.position);
var dist = lastPosition - playerObject.transform.position;
var dim = hitColliders[i].gameObject.renderer.bounds.size;
var midDimx = dim.x * 120;
var midDimy = dim.y * 120;
var finalDim = Vector2(midDimx/2,midDimy/2);
var distfinalxplus = Mathf.RoundToInt(dist.x) - Screen.width;
var distfinalyplus = Mathf.RoundToInt(dist.y) - Screen.height + 34;
distfinalx = Mathf.RoundToInt(dist.x);
distfinaly = Mathf.RoundToInt(dist.y) + 34;
if(lastPosition.x >= Screen.width){
GUI.Label(Rect(Screen.width + adjstGUI.x - 70, -lastPosition.y + adjstGUI.y + Screen.height + 20, 80,80),"Dist " + distfinalxplus);
GUI.DrawTexture(Rect(Screen.width + adjstGUI.x - 10, -lastPosition.y + adjstGUI.y + Screen.height + 20, 30,30), aTextureright, ScaleMode.ScaleToFit, true);
}
if(lastPosition.x <= 0){
GUI.Label(Rect(0 + adjstGUI.x + 95, -lastPosition.y + adjstGUI.y + Screen.height + 20, 80,80),"Dist " + -distfinalx);
GUI.DrawTexture(Rect(0 + adjstGUI.x + 60, -lastPosition.y + adjstGUI.y + Screen.height + 20, 30,30), aTextureleft, ScaleMode.ScaleToFit, true);
}
if(lastPosition.y <= 0){
GUI.Label(Rect(lastPosition.x + adjstGUI.x + 20, Screen.height + adjstGUI.y - 50, 80,80),"Dist " + -distfinaly);
GUI.DrawTexture(Rect(lastPosition.x + adjstGUI.x + 20, Screen.height + adjstGUI.y - 30, 30,30), aTextureDown, ScaleMode.ScaleToFit, true);
}
if(lastPosition.y >= Screen.height){
GUI.Label(Rect(lastPosition.x + adjstGUI.x + 20, 0 + adjstGUI.y + 95, 80,80),"Dist " + distfinalyplus);
GUI.DrawTexture(Rect(lastPosition.x + adjstGUI.x + 20, 0 + adjstGUI.y + 60, 30,30), aTextureup, ScaleMode.ScaleToFit, true);
}
GUI.DrawTexture(Rect(lastPosition.x - finalDim.x, -lastPosition.y - finalDim.y + Screen.height, 120 * dim.x,120 * dim.y),aTexture,ScaleMode.ScaleToFit,true);
}
}
}
}
}
this code is working fine with the old GUI.
3:- now i want to upgrade to NGUI which is much better but as i understand i must spawn an Object , so i did this script:
function Update () {
hitColliders = Physics.OverlapSphere(transform.position, searchrange , mask.value);
for (var i = 0; i < hitColliders.Length; i++) {
if(hitColliders[i] != null){
if(hitColliders[i].gameObject.tag == "Enemy"){
numberOfEnemyDetacted = hitColliders.Length;
if(i >= hitColliders.Length){
targetPos = Camera.main.WorldToScreenPoint(hitColliders[i].gameObject.transform.position);
var markerSpawned = Instantiate(markerUITextureObject,Vector3(targetPos.x,targetPos.y,0.0),Quaternion.identity);
markerSpawned.transform.position = cameraGUI.ScreenToWorldPoint(Vector3(targetPos.x,targetPos.y,0.0));
}
targetValue = hitColliders.Length;
}
}
}
}
this code is the problem , wt i want is to spawn an object that have a texture on multiple objects , this problems i get is: a:-using the code above just spawns soo many marker objects that lags unity and hungs. B:-i want it to spawn marker objects on only the objects near 100 range , but also removes marker objects if any objects leaves the detection range and also adds marker objects to any object enters detection range c:-marker objects just gets spawned on the center and does not follow objects
note : some of the code above have some debugging code.
thanks to anyone who tries to help me
cheers
Hi, I'm a new to unity. But looking at that code your Instantiating objects on the Update,.. That would most likely explain why it lags. I would think a better approach is to use the physics Circle/Sphere Colider, and trap for the OnTriggerEnter to create the markers, and OnTriggerExit to remove the markers.
Hi, yes.. I think your misunderstanding me. In Unity as you know the Update function gets called lots of times, in your code as it is, your creating X number of Instances of an Object (The marker),.. These objects don't disappear at the end of the frame, they will stay there until you destroy them. That's why I mentioned a more efficient approach would be detect when the Objects Enter / Leave your Circle, and create / destroy them here. With the old GUI you didn't create objects, you created simple draw commands, there were no Objects to worry about.
I achieve this with the addon to ngui : http://www.tasharen.com/forum/index.php?topic=997.0
$$anonymous$$akes ui elements following real world objects much easier to manage
The addon comes with a UIFollowTarget script that can be used for anything. Floating icons, text, names, etc. All you have to do is specifiy a real world target and it will handle the rest
When 2 & 4 leave the OnTriggerExit is fired, you can then just do Destory(other.gameObject),. To make follow just set as a child of what you want to follow, like you would in the editor. eg. markerSpawned.parent = other.
That wont work because he is translating between 3d space and the UI. The ui elements would be a child of the 3d object and would be offscreen