Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Array trouble

Discussion in 'Scripting' started by hellcaller, Apr 18, 2016.

  1. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Hi guys i'm making some kind of radar for space game. My script is collecting all the objects in a scene that should be desplayed on a radar, and instantiating a sprite on a canvas for each object. Sprites are positioned on a canvas accordig to the position of a related object via WorldToScreenPoint.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Radar : MonoBehaviour {
    7.     public Indicator[] indicators;
    8.     public GameObject indicatorPref;
    9.     public GameObject spawnedIndicator;
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void LateUpdate () {
    17.         DrawIndicator ();
    18.     }
    19.     void DrawIndicator(){
    20.         indicators = GameObject.FindObjectsOfType(typeof(Indicator)) as Indicator[];
    21.         foreach (Indicator obj in indicators) {
    22.             if(!obj.GetComponent<Indicator>().hasIndicator){
    23.                 spawnedIndicator = (GameObject)Instantiate (indicatorPref);
    24.                 spawnedIndicator.transform.SetParent(this.transform);
    25.                 obj.GetComponent<Indicator>().hasIndicator = true;
    26.             }
    27.             Vector3 screenPos = Camera.main.WorldToScreenPoint (obj.transform.position);
    28.             //check if indicator is offscreen
    29.             if(screenPos.z<0){
    30.                 spawnedIndicator.SetActive(false);
    31.             }
    32.             else{
    33.                 spawnedIndicator.SetActive(true);
    34.             }
    35.             //setting flat
    36.             screenPos = new Vector3 (screenPos.x, screenPos.y, 0);
    37.             spawnedIndicator.transform.position = screenPos;
    38.         }
    39.     }
    40. }
    41.  
    Unfortunately only first instantiated sprite is positioned correctly. The rest of the sprites are not positioned in accordance to their related objects. Can someone give me a hint what i'm doing wrong?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. Camera.main.WorldToScreenPoint(obj.transform.position);
    3.  
    that's going to be the position of the object on the current main camera, i.e. where it is in the current screen, not where it is in the map.

    Generally these "minimap"/"radar" type things are done using a second camera which is looking down at the level from above, not the main viewing camera.
     
  3. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    No, it's not a mini map it does exactly what i want it to do, the problem is it works only with the first instantiated indicator.
     
  4. Death111

    Death111

    Joined:
    Apr 17, 2016
    Posts:
    38
    Hey,

    if you put your class-variables into the function like this:

    Code (CSharp):
    1. Indicator[] indicators = GameObject.FindObjectsOfType(typeof(Indicator)) as Indicator[];
    2.         foreach (Indicator indicator in indicators)
    3.         {
    4.             GameObject spawnedIndicator;
    5.             if (!indicator.hasIndicator)
    6.             {
    7.                 spawnedIndicator = (GameObject)Instantiate(indicatorPref); // only set here
    8.                 spawnedIndicator.transform.SetParent(this.transform);
    9.                 indicator.hasIndicator = true;
    10.             }
    11.             Vector3 screenPos = Camera.main.WorldToScreenPoint(obj.transform.position);
    12.             //check if indicator is offscreen
    13.             if (screenPos.z < 0)
    14.             {
    15.                 spawnedIndicator.SetActive(false);
    16.             }
    17.             else {
    18.                 spawnedIndicator.SetActive(true);
    19.             }
    20.             //setting flat
    21.             screenPos = new Vector3(screenPos.x, screenPos.y, 0);
    22.             spawnedIndicator.transform.position = screenPos;
    23.         }
    you see, that spawnedIndicator is only set in the if statement. As soon as hasIndicator is true spawnedIndicator will always remain the same, which you do not want. You gonna need a mapping for your Indicator to Sprite. Maybe you can put it in the Indicator class, so when you set
    Code (CSharp):
    1. GameObject spawnedIndicator ;
    2. if(!indicator.hasIndicator){
    3.    //...
    4.    indicator.hasIndicator = true;
    5.    indicator.setIndicator = spawnedIndicator
    6. }
    7. spawnedIndicator = indicator.getIndicator();
    8. //..
    you should also rename your Methods. indicator.hasIndicator does not make much sense for me :)
     
  5. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Thanx for trying to help, but i don't get it...
     
  6. Death111

    Death111

    Joined:
    Apr 17, 2016
    Posts:
    38
    you have a class member named spawnedIndicator.
    Code (CSharp):
    1. public class Radar : MonoBehaviour {
    2.     public Indicator[] indicators;
    3.     public GameObject indicatorPref;
    4.     public GameObject spawnedIndicator; // <-
    in your method you use
    Code (CSharp):
    1. spawnedIndicator.transform.position = screenPos; // update position of spawnedIndicator for your radar
    but you only assign this variable in this if statement
    Code (CSharp):
    1. if(!obj.GetComponent<Indicator>().hasIndicator){
    2.                 spawnedIndicator = (GameObject)Instantiate (indicatorPref);
    after the first call of your method, you will never reach this code again. So spawnedIndicator is always the same, but you should update the reference everytime.
     
  7. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    Got it, thanx! Now i'm using a pool of indicator sprites and it works great! thnx again
     
    Death111 likes this.