• 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
Question by DizzyTornado · Jan 29, 2017 at 01:12 AM · c#scripting problemupdateif-statementsupdate function

Update within an if statement not working?

I need to use an Update method in an if statement, I know its inefficient, but it is necessary for this project, unfortunately though, I get this error; Error CS0103 The name 'Update' does not exist in the current context. This is my code;

 public class BillboardScript : MonoBehaviour
 {
     public Transform BillboardCamera;
     public bool UpdateEveryFrame;
 
     public void Awake()
     {
         if (UpdateEveryFrame == true)
         {
             Update();
             {
                 transform.LookAt(BillboardCamera);
             }
         }
         else
         {
             Awake();
             {
                 transform.LookAt(BillboardCamera);
             }
         }
     }
 }
Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Jan 29, 2017 at 06:32 AM

You can't dynamically declare class methods inside methods. That's impossible from the very underlying class system.

If all that script is doing is to align the object "once" or every update, you can simply do:

 public class BillboardScript : MonoBehaviour
 {
     public Transform BillboardCamera;
     public bool UpdateEveryFrame;
  
     void Start()
     {
         transform.LookAt(BillboardCamera);
         if (!UpdateEveryFrame)
             Destroy(this);
     }
     
     void Update()
     {
         transform.LookAt(BillboardCamera);
     }
 }

This will simply update your object every frame. However when "UpdateEveryFrame" is false the script destroys itself after the first alignment. If you need the object to be "resettable" you could simply set enabled = false; instead of destroying the script. Though Awake and Start only run once in the lifetime of an instance so "resetting" makes not much sense.

Comment
Billytheme
ifdef_ben
DizzyTornado

People who like this

3 Show 0 · 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

Answer by whereswaldo · Jan 29, 2017 at 12:53 PM

try using void update instead of update

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
  public class BillboardScript : MonoBehaviour
  {
      public Transform BillboardCamera;
      public bool UpdateEveryFrame;
     bool update = false;
  
      public void Awake()
      {
          if (UpdateEveryFrame == true)
          {
             update = true;
          }
          else
          {
              Awake();
              {
                  transform.LookAt(BillboardCamera);
              }
          }
      }
 
     void Update()
     {
         if (update == true)
         {
             transform.LookAt(BillboardCamera);
         }
     }
  }
 



Comment

People who like this

0 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 fafase · Jan 30, 2017 at 07:18 AM 1
Share

First the curly braces are useless but this is doomed.

If UpdateEveryFrame is true then call awake, in awake, if UpdateEveryFrame is true then call awake, in awake...

You get the idea?

avatar image

Answer by ionside · Jan 29, 2017 at 01:24 AM

Why don't you use the bool in both Awake() and Update()?

Also, Awake() is always called before Update(), at the very start of your scene, your if/else statement just won't work.

 void Awake(){
     if(!UpdateEveryFrame)
         transform.LookAt(BillboardCamera);
 }
 
 void Update(){
     if(UpdateEveryFrame)
         transform.LookAt(BillboardCamera);
 }


Comment

People who like this

0 Show 2 · 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 DizzyTornado · Jan 29, 2017 at 01:35 AM 0
Share

I cant do this because it would still check the if statement is true every second and in this project I need everything to be optimized perfectly because this script will be ran hundreds of thousands of times, if not more.

avatar image ionside DizzyTornado · Jan 29, 2017 at 10:23 PM 0
Share

I see, what if you create a second bool, which changes to true then the if statement can include check on the two bools?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

292 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

Related Questions

Play method only ONCE when detected by the update function 2 Answers

If condition true but code is not executed 0 Answers

How to tell an operand "Once" or "Started to" 3 Answers

Problem with transform.LookAt and if statement 1 Answer

Disable an if statement in update 3 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