Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Sarthak123 · Oct 25, 2014 at 09:50 AM · vector3optimizationsetactiveco-routine

How To Set Active/Deactivate A GameObject after calculating it's Distance from the player..?

To reduce the force on CPU and make my game run smooth i created a script and attached it to every object which i wanted to be deactivated once the players moves away and activated again once the player comes near, this was the script.

private var Player : Transform;

 function Start () {
 
 }
 
 function Update () {
 Player = GameObject.FindGameObjectWithTag("Player").transform;
 
 if(Vector3.Distance(Player.position, transform.position) > 20)
 {
     gameObject.SetActive(false);
 }
 
 if(Vector3.Distance(Player.position, transform.position) < 20)
 {
     gameObject.SetActive(true);
 }
 
 }

soon i founded that it was not a good idea as a deactivated object will not be able to activate itself again,

so is there, any way to make this script work? or am i missing something? i know there's a solotion that i could attach those objects to a parent empty object and attach this script to the parent object and make it run checking every child's pos. but i cant due to some reasons hence i need another solution..!

any help is appreciated...

Comment
Add comment · Show 2
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 Itaros · Oct 25, 2014 at 10:04 AM 0
Share

Btw, "Player = GameObject.FindGameObjectWithTag("Player").transform;" is huge performance hit. Consider perfor$$anonymous$$g that in Start()

avatar image Sarthak123 · Oct 25, 2014 at 10:30 AM 0
Share

but then how can i get player's position every now and then to set activ it once player cmes near?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Itaros · Oct 25, 2014 at 10:01 AM

This type of optimization is performed by special objects called managers. They have scripts to control when and what activates and deactivates. There is no common way of doing them due to how overall game-design can change the possibility of using various optimization techniques, but in general it something like this:

 public class DistanceCullingManager : MonoBehaviour{
 public List<GameObject> trackables = new List<GameObject>();
 
 void Update(){
 foreach(GameObject go in trackables){
 //Disabling/Enabling code there
 }
 }
 }

Take note this is the worst way of doing it, but I hope this will show the general idea.

Most optimization to this include per-frame enumeration stepping, listing only disabled objects(which are registered when disabled) and so on. Be creative ;)

Comment
Add comment · Show 3 · 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 Sarthak123 · Oct 25, 2014 at 10:27 AM 0
Share

mm,.. cant really understand the code cause i am new to co-routines, i do not use co-routines much, i think i will go with anyother idea.. thanks anyways for the help..!

avatar image Itaros · Oct 25, 2014 at 11:20 AM 0
Share

There is no coroutine xD

avatar image Sarthak123 · Oct 25, 2014 at 12:45 PM 0
Share

foreach is not a co-rutine? sorry i thought it is a. as i said i dont know about co-routines...lol

avatar image
0

Answer by Diablo404 · Oct 25, 2014 at 09:57 AM

As the doc says: http://docs.unity3d.com/ScriptReference/GameObject.SetActive.html

Any scripts that you have attached to the GameObject will no longer have Update() called, for example.

So if you want to do this, consider storing all of the gameobjects that you want to check in a array, and have a script atached to the player for example, which could check every item of the array regarding their position and the enable state. If you want some help about this, just ask

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 Sarthak123 · Oct 25, 2014 at 10:26 AM 0
Share

but in my scene many objects are intantiated time by time, some have tags some do not, some are many in numberm, some are unique, how can i add them all to the array? is there anyway to create a similarity between them and then check if the object has that similarity or not?

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

29 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

Related Questions

Calculating the Exact Center of Multiple Objects 2 Answers

physics.OverlapSphere colliders 1 Answer

Game optimization using .SetActive() slowing down game. 0 Answers

Optimizing : Set Active or Clipping Planes? 1 Answer

Can you suggest a faster way to rotate an object? 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