Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MigLeader · Oct 10, 2014 at 04:04 PM · guiphysicsinstantiatearrayoverlapsphere

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

Comment
Add comment · Show 17
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KpjComp · Oct 10, 2014 at 08:17 PM 1
Share

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.

avatar image KpjComp · Oct 10, 2014 at 11:28 PM 1
Share

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.

avatar image b1gry4n · Oct 11, 2014 at 01:44 AM 1
Share

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

avatar image KpjComp · Oct 11, 2014 at 07:54 AM 1
Share

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.

avatar image b1gry4n · Oct 11, 2014 at 09:40 AM 1
Share

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

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom Inspector GUI, Instantiating some classes 2 Answers

Whats Wrong? 1 Answer

Confused using Arrays 1 Answer

How do I reference a GameObject after finding its collider with Physics.OverlapSphere()? 1 Answer

Hover Effect with more rays as turbines? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges