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 Merglyn · Feb 24, 2013 at 05:05 AM · basics

Auto Enabling and looping a script?

Hey guys, I'm a noobie at Unity and the syntax used for scripting. I know a little javascript so I wrote what I could of a script that could disable and enable a camera with a little delay. Here's what I came up with.

 function Start()
 {
       GameObject.Find("Main Camera").camera.enabled = false;
       yield WaitForSeconds(.05);
       GameObject.Find("Main Camera").camera.enabled = true;
       yield WaitForSeconds(5);   
 }

Problem is, I have no clue where to go from there, as I need to have it loop and be running from the start. Any help? :D

Thanks, and sorry for being such a noob (sidenote, if someone could point me to some good basic video tutorials that would be great)

--Merglyn

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 AlucardJay · Feb 24, 2013 at 05:10 AM 0
Share

Here is a list of tutorials to get you going :

  • Start at the bottom and work up : http://www.unity3dstudent.com/category/modules/

  • the Unity Wiki : http://wiki.unity3d.com/index.php/Tutorials

  • A list of resources : http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html

Also look at the Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/index.html

use the search bar in the top-left, eg type in InvokeRepeating then click on the first link : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html

this is one way to loop something, but usually anything that keeps getting executed is placed in an Update function :

 function Update() 
 {
     // do stuff every frame
 }
avatar image Merglyn · Feb 24, 2013 at 06:01 AM 0
Share

This helped a bunch. Thanks.

The script reference helped a lot with Unity specific commands, but it was the general syntax that I was confused about... Thanks a bunch for the tuts though!

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Toxic Blob · Feb 24, 2013 at 05:24 AM

CoRoutines are superbly helpful for this. Of course if you want to improve the performance of the script, you'll want to find your main camera in Start (so it happens only once). Cache the found camera to a variable, then access it later in LoopCamera().

     function Start()
         {
         StartCoroutine( LoopCamera() );
         }
     function LoopCamera() : IEnumerator
        {
        while( true )
            {
            GameObject.Find("Main Camera").camera.enabled = false;
            yield WaitForSeconds(.05);
            GameObject.Find("Main Camera").camera.enabled = true;
            yield WaitForSeconds(5);   
            }
        }


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 Merglyn · Feb 24, 2013 at 06:02 AM 0
Share

Perfect. I had seen CoRoutines in other posts on similar topics, but they didn't give any specifics on the syntax and how they worked... Thanks for the answer!

avatar image Merglyn · Feb 24, 2013 at 07:49 PM 0
Share

Another comment, it seems to work fine in the preview, but when I build and run it, it doesn't do anything... Any help?

avatar image Merglyn · Feb 24, 2013 at 08:35 PM 0
Share

Never$$anonymous$$d. Preview defaulted disabled cameras to black while build defaulted it to last frame. Ended up enabling another camera that was just aimed at a plane. Thanks again for the 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

11 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

Related Questions

How to declare and access other scripts publicly in Csharpe? 3 Answers

How to make walls slowly close in on player? 1 Answer

Access script (not derived from mono) functions in another script 2 Answers

Generate Chart with Data 0 Answers

Fixing a single call of an action (across 2 scripts) 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