- Home /
Drawing GUI onto multiple objects using for loops?
As the title may suggest, the problem I am currently having is this: I can't find a way to make GUI draw onto multiple objects. I tried what I have below, but it didn't work, probably because I'm not referencing the "enemies" object. I have no idea where I would be able to put the "enemies" object, since putting it behind the GUI.DrawTexture gives me a "Null Reference Exception" error.
for(var enemies : GameObject in Enemies) {
if(LockedOn == false) {
// GUI.DrawTexture(Rect (screenPos.x-10, screenPos.y-12, 25, 25), EnemyOutline);
}
}
Answer by PAEvenson · Dec 18, 2012 at 01:51 PM
Maybe you are looking for something like this? Have not tested...but should point you in the right direction.
var enemyScreenPos = Camera.current.WorldToScreenPoint(enemies.transform.position);
GUI.DrawTexture(Rect (enemyScreenPos.x-10, enemyScreenPos.y-12, 25, 25), EnemyOutline);
God I am stupid.....I forgot that my screenPos was referring to the target, not the entire group of enemies.... With a few tweaks it worked, thanks alot :)
If anybody is wondering, this is the changed code, it might help you in your game :D Special thanks to @PAEvenson :) I added a few comments so that you arent confused.
for(var enemies : GameObject in Enemies) {
if(enemies != null) {
if(enemies.renderer.isVisible != false) {
//if enemies are visible
if(LockedOn == false) {
//if we are locked on
var enemyScreenPos = Camera.current.WorldToScreenPoint(enemies.transform.position);
enemyScreenPos.y = Screen.height - enemyScreenPos.y;
GUI.DrawTexture(Rect (enemyScreenPos.x-10, enemyScreenPos.y-12, 25, 25), EnemyOutline);
}
}
}
}
Its fine, I just remembered that in the old Answers, code wouldn't display properly(It would come out as a jumble of code, not neatly formatted). At least they fixed it now :D I wouldn't $$anonymous$$d some people being slightly mean towards me, I too would get annoyed if someone didn't know how to post a question properly. That still doesn't give them the right to be arrogant d**k heads though :)
Your answer
Follow this Question
Related Questions
NullReferenceException in for-loop 0 Answers
Change Button GUIStyle on Click 1 Answer
Using for ... var ... in loop to access variable. 1 Answer
How can I make my for loop "reset" if my array contains an value? 5 Answers
Creating Dropdown GUI 1 Answer