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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Coding Help Needed - Disable/Enable Objects inside Radius

Discussion in 'Scripting' started by Mr_fuzion, Mar 20, 2018.

  1. Mr_fuzion

    Mr_fuzion

    Joined:
    Jul 3, 2013
    Posts:
    10
    Hi everyone, need some help with my code I have been trying to optimise my game as there is the possibility to have 1000's of objects within the scene at once and it is for mobile. All these objects run a constant Update() method that cause lag to combat this I am disabling the objects outside the players renderRadius however enabling them if they are within the radius. This code is working perfectly however I also want my Enemies to contain the script. For a singular enemy I have edited the script and it works however I plan to add this script to multiple enemies but in doing so no planets are enabled within the enemies radius unless all the enemies radius' are overlapping. This is due to the lines of code on both 36 and 63 but if i remove this it allows the enemies to enable new planets as they enter the radius however they cannot disable the planets as they exit the radius. Code Below.

    PlanetActivationCode.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlanetActivationEnemy : MonoBehaviour {
    6.  
    7.     public GameObject[] planetLifesInRange;
    8.     public List<GameObject> listOfPlanetLifesInRange = new List<GameObject>();
    9.     public List<GameObject> listOfPlanetCarbonsInRange = new List<GameObject>();
    10.     public GameObject[] planetCarbonsInRange;
    11.     public GameObject parent;
    12.     public float renderDistance = 100f;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.  
    17.         parent = this.gameObject;
    18.         Invoke ("ListPlanets", 1f);
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.  
    25.         foreach (GameObject planetLife in planetLifesInRange) {
    26.             if (planetLife != null) {
    27.                 float dist = (parent.transform.position - planetLife.transform.position).magnitude;
    28.                 if (dist < renderDistance) {
    29.                     if (!listOfPlanetLifesInRange.Contains (planetLife)) {
    30.                         listOfPlanetLifesInRange.Add (planetLife);
    31.                         planetLife.SetActive (true);
    32.                         planetLife.GetComponent<PlanetLife> ().inEnemyRender = true;
    33.                     }
    34.                 } else {
    35.  
    36.                     planetLife.GetComponent<PlanetLife> ().inEnemyRender = false;
    37.                    
    38.                     if (planetLife.GetComponent<PlanetLife> ().inPlayerRender == false && planetLife.GetComponent<PlanetLife> ().inEnemyRender == false) {
    39.                         listOfPlanetLifesInRange.Remove (planetLife);
    40.                         planetLife.GetComponent<PlanetLife> ().inEnemyRender = false;
    41.                         planetLife.GetComponent<PlanetLife> ().inPlayerRender = false;
    42.                         planetLife.SetActive (false);
    43.                     }
    44.                 }
    45.             }else {
    46.                 listOfPlanetLifesInRange.Remove (planetLife);
    47.  
    48.             }
    49.         }
    50.  
    51.  
    52.         foreach (GameObject planetCarbon in planetCarbonsInRange) {
    53.             if (planetCarbon != null) {
    54.                 float dist = (parent.transform.position - planetCarbon.transform.position).magnitude;
    55.                 if (dist < renderDistance) {
    56.                     if (!listOfPlanetCarbonsInRange.Contains (planetCarbon)) {
    57.                         listOfPlanetCarbonsInRange.Add (planetCarbon);
    58.                         planetCarbon.SetActive (true);
    59.                         planetCarbon.GetComponent<PlanetCarbon> ().inEnemyRender = true;
    60.                     }
    61.                 } else {
    62.  
    63.                     planetCarbon.GetComponent<PlanetCarbon> ().inEnemyRender = false;
    64.                    
    65.                     if (planetCarbon.GetComponent<PlanetCarbon> ().inPlayerRender == false && planetCarbon.GetComponent<PlanetCarbon> ().inEnemyRender == false) {
    66.                         listOfPlanetLifesInRange.Remove (planetCarbon);
    67.                         planetCarbon.GetComponent<PlanetCarbon> ().inEnemyRender = false;
    68.                         planetCarbon.GetComponent<PlanetCarbon> ().inPlayerRender = false;
    69.                         planetCarbon.SetActive (false);
    70.                     }
    71.                 }
    72.             } else {
    73.                 listOfPlanetCarbonsInRange.Remove (planetCarbon);
    74.  
    75.             }
    76.         }
    77.     }
    78.  
    79.     void ListPlanets(){
    80.         planetLifesInRange = GameObject.FindGameObjectsWithTag("PlanetLife");
    81.         planetCarbonsInRange = GameObject.FindGameObjectsWithTag ("PlanetCarbon");
    82.     }
    83.  
    84.     void OnDrawGizmosSelected(){
    85.  
    86.         Gizmos.color = Color.white;
    87.         Gizmos.DrawWireSphere (this.transform.position, renderDistance);
    88.  
    89.     }
    90.  
    91. }
    92.  
    PlanetLife.cs Lines 6-9
    Code (CSharp):
    1. public class PlanetLife : MonoBehaviour {
    2.  
    3.     public bool inPlayerRender = false;
    4.     public bool inEnemyRender = false;
    5.  
    PlanetLife.cs Lines 78 - 84
    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update (){
    3.  
    4.         if (inPlayerRender == true || inEnemyRender == true) {
    5.             InPlayerRender ();
    6.         }
    7.     }
     
  2. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    As for the problem with them not becoming active until every enemy overlaps, that's because they aren't checking if the object is in someone's vision before disabling them. You'll need another boolean value, or something like that, which marks the planet as in vision. Then They are only disabled when both out of the object's range and out of everyone's range.

    However, I noticed that you're using a GetComponent call inside a loop multiple times per object. That's going to be a massive resource cost alone. Each one of those GetComponent calls has O(n) complexity I believe, so you should try to treat as their own contained loops when it comes to performance.

    I would suggest having a singular script that handles this logic for everyone. To save time, you'll need to store a list of each deactivateable component populated by a single GetComponent loop in the Start method, as well as a list of all the PlayerEnemy transforms. Then you can just loop through these existing loops directly instead of going through generic GameObjects and getting their Components on every use of the component.

    Now, if you did this on every Player/Enemy script, your memory use would balloon, so this is a case where it would probably be best for a singular object to be responsible for it. In addition, this means your player objects aren't all trying to maintain refererences to everything. So if you want to remove anything from the list, you only have to do it once.
     
    Mr_fuzion likes this.
  3. Mr_fuzion

    Mr_fuzion

    Joined:
    Jul 3, 2013
    Posts:
    10
    Thank you for the advice and assistance ill give it a go now its really appreciated :)