Search Unity

Area detector ?

Discussion in 'Scripting' started by skyLark9, Aug 16, 2019.

  1. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    I'm trying to make indicator detect a certain objects in my scene. Where every arrow point at separate object. for example, face with two eyes. my script will look for those eyes"dead_point" and clone the image "original_arrow" as the number of eyes. Then rotate each image separately at each eyes. My issue was with "Arrow_clone[y]" and "PointingAt[y]" both have the same error "Index was outside the bounds of the array."

    Code (CSharp):
    1.    
    2.     public GameObject original_arrow; // image
    3.     public GameObject[] attackedArea,Arrow_clone;
    4.     public Vector3[] PointingAt;
    5.     private float xtimer = 0.8f;
    6.     public int y;
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         attackedArea = GameObject.FindGameObjectsWithTag("dead_point");
    11.         y = attackedArea.Length;
    12.  
    13.         if (attackedArea != null)
    14.         {
    15.                 Arrow_clone[y] = Instantiate(original_arrow, original_arrow.transform.position, original_arrow.transform.rotation); // error "Index was outside the bounds of the array."
    16.                 PointingAt[y] = attackedArea[y].transform.position; // error "Index was outside the bounds of the array."
    17.  
    18.                 Vector3 dir = Camera.main.WorldToScreenPoint(attackedArea[y].transform.position);
    19.                 PointingAt[y].z = Mathf.Atan2((Arrow_clone[y].transform.position.y - dir.y), (Arrow_clone[y].transform.position.x - dir.x)) * Mathf.Rad2Deg - 90;
    20.                 Arrow_clone[y].transform.rotation = Quaternion.Euler(PointingAt[y]);
    21.  
    22.                 float pingPong = Mathf.PingPong(Time.time * xtimer, 1);
    23.                 Arrow_clone[y].GetComponent<CanvasGroup>().alpha = Mathf.Lerp(0, 1, pingPong);
    24.            
    25.         }
    26.  
    27.     }
    28.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The error is being thrown because this
    y
    value is greater than the amount of elements in the arrays, meaning you're trying to select an element from an index that doesn't exist.

    Your usage of arrays here is really confusing me. I'm not sure why you're using a pre-set index value (
    y
    ) from the inspector to select elements in multiple arrays. What is this value supposed to represent and why do you need it? Are you certain that each of these arrays will have
    y
    amount of elements inside them?

    Perhaps you want to loop through all elements in each array instead?
     
  3. skyLark9

    skyLark9

    Joined:
    May 2, 2018
    Posts:
    135
    OK. Look at this script.
    Code (CSharp):
    1.     public GameObject original_arrow; // arrow object
    2.     public GameObject attackedArea;
    3.     public Vector3 PointingAt;
    4.     private float xtimer = 0.8f;
    5.     // Update is called once per frame
    6.     void Update()
    7.     {
    8.         attackedArea = GameObject.FindGameObjectWithTag("dead_point");
    9.  
    10.         if (attackedArea != null)
    11.         {
    12.  
    13.                 Vector3 dir = Camera.main.WorldToScreenPoint(attackedArea.transform.position);
    14.                 PointingAt.z = Mathf.Atan2((original_arrow.transform.position.y - dir.y), (original_arrow.transform.position.x - dir.x)) * Mathf.Rad2Deg - 90;
    15.                 original_arrow.transform.rotation = Quaternion.Euler(PointingAt);
    16.  
    17.                 float pingPong = Mathf.PingPong(Time.time * xtimer, 1);
    18.                 original_arrow.GetComponent<CanvasGroup>().alpha = Mathf.Lerp(0, 1, pingPong);
    19.            
    20.         }
    21.  
    22.     }
    In this script, the arrow object will move to face the attackedArea. In my case, There is more than 1 area in different places in my scene and I can't use one arrow object for them all.
    To solve this I used array to to list my areas into. Then clone arrow object as the number of areas in my array list. If I have 3 "attackedArea" in my scene my script will clone arrow object 3 times. Before I used "for" function but did not work.
    Code (CSharp):
    1.             for (int y = 0; y < attackedArea.Length; y++)
    2.             {
    3.                 print("dead_point y " + y);
    4.                 Arrow_clone[y] = Instantiate(original_arrow, original_arrow.transform.position, original_arrow.transform.rotation);
    5.                 PointingAt[y] = attackedArea[y].transform.position;
    6.  
    7.                 Vector3 dir = Camera.main.WorldToScreenPoint(attackedArea[y].transform.position);
    8.                 PointingAt[y].z = Mathf.Atan2((Arrow_clone[y].transform.position.y - dir.y), (Arrow_clone[y].transform.position.x - dir.x)) * Mathf.Rad2Deg - 90;
    9.                 Arrow_clone[y].transform.rotation = Quaternion.Euler(PointingAt[y]);
    10.  
    11.                 float pingPong = Mathf.PingPong(Time.time * xtimer, 1);
    12.                 Arrow_clone[y].GetComponent<CanvasGroup>().alpha = Mathf.Lerp(0, 1, pingPong);
    13.             }
    Yes, something wrong But i don't why my "Arrow_clone[y]" and "PointingAt[y]" not getting their values from attackedArea list !?