Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Lord_Arcanis · Mar 18, 2017 at 07:26 PM · c#setactive

Why won't these objects deactivate?

The code on deactivating that I am referencing is at the bottom, It is supposed to deactivate the objects if they are behind the camera, which follows the player. Though when the rows that are activated are behind the camera's point of view or anywhere for that matter just don't disable after I have activated them.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RowSpawner : MonoBehaviour
 {
     public bool isAlive;
     float xPos;
     Vector3 rowSpot;
     public int random;
     public GameObject rowOf1;
     public GameObject rowOf2;
     public GameObject rowOf3;
     public GameObject rowOf4;
     public int pooledAmount;
     public List<GameObject> row1;
     public List<GameObject> row2;
     public List<GameObject> row3;
     public List<GameObject> row4;
     public float back;
     public float front;
     public GameObject cameraUI;
     private float cameraPos;
 
     void Start ()
     {
         row1 = new List<GameObject> ();
         row2 = new List<GameObject> ();
         row3 = new List<GameObject> ();
         row4 = new List<GameObject> ();
         isAlive = true;
         InvokeRepeating ("Spawner", 2.0F, 2.0F);
         xPos = 0;
         for (int i = 0; i < pooledAmount; i++) {
             GameObject obj1 = (GameObject)Instantiate (rowOf1);
             obj1.SetActive (false);
             row1.Add (obj1);
             GameObject obj2 = (GameObject)Instantiate (rowOf2);
             obj2.SetActive (false);
             row2.Add (obj2);
             GameObject obj3 = (GameObject)Instantiate (rowOf3);
             obj3.SetActive (false);
             row3.Add (obj3);
             GameObject obj4 = (GameObject)Instantiate (rowOf4);
             obj4.SetActive (false);
             row4.Add (obj4);
         }
     }
 
     void Update ()
     {
         cameraPos = cameraUI.transform.position.x;
         back = cameraPos - 9.75F;
         front = cameraPos + 9.75F;
         Destroyer ();
     }
 
     void Spawner ()
     {
         random = Random.Range (1, 5);
         xPos = xPos + 8F;
         rowSpot = new Vector3 (xPos, 0, 0);
         if (isAlive == true) {
             if (random == 1) {
                 for (int i = 0; i < row1.Count; i++) {
                     if (!row1 [i].activeInHierarchy) {
                         row1 [i].transform.position = rowSpot;
                         row1 [i].transform.rotation = Quaternion.identity;
                         row1 [i].SetActive (true);
                         break;
                     }
                 }
             }
             if (random == 2) {
                 for (int i = 0; i < row2.Count; i++) {
                     if (!row2 [i].activeInHierarchy) {
                         row2 [i].transform.position = rowSpot;
                         row2 [i].transform.rotation = Quaternion.identity;
                         row2 [i].SetActive (true);
                         break;
                     }
                 }
             }
 
             if (random == 3) {
                 for (int i = 0; i < row3.Count; i++) {
                     if (!row3 [i].activeInHierarchy) {
                         row3 [i].transform.position = rowSpot;
                         row3 [i].transform.rotation = Quaternion.identity;
                         row3 [i].SetActive (true);
                         break;
                     }
                 }
             }
 
             if (random == 4) {
                 for (int i = 0; i < row4.Count; i++) {
                     if (!row4 [i].activeInHierarchy) {
                         row4 [i].transform.position = rowSpot;
                         row4 [i].transform.rotation = Quaternion.identity;
                         row4 [i].SetActive (true);
                         break;
                     }
                 }
             }
         }
 
     }
 
     public void Destroyer ()
     {
         for(int i = 0; i < row1.Count; i++){
             if (row1 [i].transform.position.x < back && row1[i].activeInHierarchy) {
                 row1 [i].SetActive (false);
                 break;
             }
         }
 
         for(int i = 0; i < row2.Count; i++){
             if (row2 [i].transform.position.x < back && row2[i].activeInHierarchy) {
                 row2 [i].SetActive (false);
                 break;
             }
         }
 
         for(int i = 0; i < row3.Count; i++){
             if (row3 [i].transform.position.x < back && row3[i].activeInHierarchy) {
                 row3 [i].SetActive (false);
                 break;
             }
         }
 
         for(int i = 0; i < row4.Count; i++){
             if (row4 [i].transform.position.x < back && row4[i].activeInHierarchy) {
                 row4 [i].SetActive (false);
                 break;
             }
         }
     }
 }
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
1

Answer by Mine54i__ · Mar 18, 2017 at 10:40 PM

Excuse me if I do not understand what you are trying to achieve, but from what it sounds like you are trying to disable all of the objects behind the camera to boost performance? If so, I would just use occlusion culling, which is built into Unity. You can find great tutorials and it is so much simpler than creating a script for it. Hope this helps :)

Comment
Add comment · Show 1 · 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 Lord_Arcanis · Mar 18, 2017 at 11:31 PM 0
Share

Though it is similar, I am trying to disable items in the object pool if they are moved out of the camera so they can be reused. I need to be able to reuse these objects ins$$anonymous$$d of making more because this is for a mobile game and it needs to be optimized. Instantiating a ton of new objects isn't really great for optimization.

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

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

Regarding SetActive function 1 Answer

Disable Object and Enable by Distance from Player 1 Answer

SetActive delay between code and actual object on the screen 0 Answers

My Game Over Canvas is working in one level but not the other! 0 Answers

Unable to reactivate an object that's been deactivated. C# 4 Answers


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