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 Azial · Mar 20, 2013 at 07:31 PM · instantiatefunctionnewocclusion culling

"Instantiate"/make new function via script?

Hi,

is it possible to somehow make a new function within the same script? It can be the same function with different parameters, so maybe there's a way like instantiating it like a object from a class?

Background: I came up with the idea of making a simple occlusion culling for Unity free. Currently I make an empty gameobject and attach my following script for each object-(group) I want to cull:

 var toCul : GameObject; //(group of) object(s) we want to be active only when we are in...
 var culRange : int = 100; //...this range
 
 function Awake() {
     toCul.SetActive(false); //deactivate the "problem"-object(s) at start
 }
 
 CheckRange(0);
 
 function CheckRange(waitFor : int) : IEnumerator {
     yield WaitForSeconds(waitFor);
     var curRange : int = Vector3.Distance(Camera.main.transform.position, toCul.transform.position); //range from the camera to the object
     if (curRange < culRange) {
         toCul.SetActive(true); //if in reach activate the object...
     } else {
         toCul.SetActive(false); //if not, deactivate it
     }
     var checkIn = Mathf.Max(0.5, 5*curRange/culRange); //the next check depends on the range from the camera to the object (for better performance)
     CheckRange(checkIn);
 }


It works great and currently I just have 3 of them on my empty gameobject. But I wonder how I could extend this to work on just one single script so I could store all my objects i want to cull in an array and for each index there's automaticly made a "CheckRange"-function like in the code?

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
0
Best Answer

Answer by Loius · Mar 20, 2013 at 07:45 PM

Just re-write InvokeRepeating in your manager.

 var objs: Cullable[] = whatever;
 
 InvokeRepeating(CheckRanges...);]
 
 function CheckRanges() {
   for ( var i in objs ) {
     i.CheckRange();
   }
 }
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 Azial · Mar 21, 2013 at 01:21 AM 0
Share

Thx, but maybe I was not so clear: I have this script above not attched on the objects that I want to cull, I have the scripts all on one single gameobject. In the inspector I can now drag may objects which I want to cull on the "toCul" variable.

If I interprete your script right, you would access the function in my script, if the objects I want to cull had my script attched. But this is not the case and can't work because I want to activate/deactivate these objects complete, so I can't attach some sort of check-range script or similar.

avatar image Loius · Mar 21, 2013 at 07:30 PM 0
Share
 var objs: Cullable[] = whatever;
 
 InvokeRepeating(CheckRanges...);]
 
 function CheckRanges() {
   for ( var i in objs ) {
     // insert your CheckRange function here but replace 'this' with 'i', so 'transform.position' becomes 'i.transform.position'
   }
 }
avatar image Azial · Mar 23, 2013 at 09:20 PM 0
Share

Ok, now I understand, but I should make also a new list for the ranges. It's more like a workaround for my problem, but for others this would probably help, so I mark as correct answer :)

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

12 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

Related Questions

Instantiate A Prefab 1 Answer

function Start () problem!! 3 Answers

The Walking Dead Game Animation Style 1 Answer

Calling a Function from other script ( C# ) 1 Answer

Questions of the Coroutines 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