Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by MigLeader · Apr 23, 2016 at 10:50 PM · instantiatearrayuser interfaceradar

Instantiate and remove objects according to the number of objects in an Array

hello everyone , i got a bit of a problem , here it is:

i want to create a radar , where it have a range , and some planets , what i want is as follows:

1:-when a planet enters the range (lets say 100) its added to an array (Done)

2:-spawn a UI image object for each planet in range , so if there is 3 planets in range then spawn only 3 , if 2 then spawn 2 .....etc

3:-if a planet leaves the range , then its corresponding icon gets remove (maybe Disabled).

4:-make each icon follows its corresponding planet (will try to do it on my own at first)

problem: using the script below , its just adds unlimited number of Icons.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Radar : MonoBehaviour {
 
     public float radarRange;
     public LayerMask planets;
     public Collider[] hits;
     public Slider range;
 
     public Image icon;
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         //Collider[] hits;
         hits = Physics.OverlapSphere(transform.position, radarRange, planets);
         radarRange = range.value;
 
         foreach (Collider planetObject in hits)
         {
             if (Vector3.Distance(transform.position, planetObject.transform.position) < radarRange)
             {
                 Instantiate(icon, planetObject.transform.position, Quaternion.identity);
             }
         }
     }
 }


i know it might look like a beginner script , but i am just learning to script.

thanks for any help i get.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheSorm · Apr 24, 2016 at 02:38 AM

Look at this: http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html Your code dont work because the collider is called every fixed update and look if ther is a collsion but the collision dont stop after one collison so many instanzien get startet. If you would use Collider.OnTriggerEnter and Collider.OnTriggerExit it would just called once.

Comment
Add comment · Show 2 · Share
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 MigLeader · Apr 25, 2016 at 06:46 AM 0
Share

hmm , thanks for the respond , but i don't see how this gonna help me , as for the detection of planets , i am using Physics.OverlapSphere, not a normal collision detection.

anyway thanks for your time

avatar image TheSorm MigLeader · Apr 27, 2016 at 06:12 PM 0
Share

if you use Triggers its way easyer to detect when an object enter and leaf your radius.

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

50 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 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

How can i Instantiate gameobject in array 0 Answers

Adding a new GameObject to an Array without overriding the others 0 Answers

Can I spawn with an array in order? 2 Answers

How do I destroy a instantiated UI image that is a prefab but pushed the canvas? 0 Answers

Need Help instantiating GameObjects in an array using the middle mouse wheel. whew. 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