• 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 andrewmaples · Jan 11, 2013 at 11:18 AM · namespace

Minor hang-up with code

I'm stuck on this little hang-up, and I can't figure out why Unity won't compile this script, except for the fact that Visual Studio seems to expect me to end my start() function after the first line. I'm just starting to learn C#, so any help/advice/criticism would be much appreciated!

Visual Studio underlines the semi-colon following "float movementSpeed = 1" and says "} expected" and underlines the word void in front of update() and says "A namespace cannot directly contain members such as fields or methods."

 using UnityEngine;
 using System.Collections;
 
 public class SoldierControl : MonoBehaviour
 {
     void start() 
 
 { 
     
             float movementSpeed = 1;
             public float jumpHeight = 1;
             public int lowerLimit = -10;
             int health = 100;
             int yPos = transform.position.y;
             bool isGrounded = false;
     }
 
     void update()
     {
             if (Input.GetAxis("Horizontal") == 1)
                 moveRight();
             if (Input.GetAxis("Horizontal") == -1)
                 moveLeft();
             if (Input.GetAxis("Jump") == 1 && isGrounded)
                 jump();
 
         if (yPos < lowerLimit || health < 1)
             kill();
     }
 
     void moveRight()
     {
         rigidbody.AddForce(Vector3.right * movementSpeed);
     }
 
     void moveLeft()
     {
         rigidbody.addForce(Vector3.left * movementSpeed);
     }
 
     void jump()
     {
         rigidbody.addForce(Vector3.up * jumpHeight);
     }
     void kill()
     {
         destroy();
     }
 }
 
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
Best Answer

Answer by robertbu · Jan 11, 2013 at 11:58 AM

The declaration of your public variables need to be at the class level, not inside the Start() method (which is why you receive the error). The declaration of your other variables probably also should be moved outside of the Start() method since they pass out of scope as soon as Start() is finished.

Though what you have here will work, you may want to initialize your floating point variables with floats rather than ints. For exmaple:

 public float jumpHeight = 1.0f;

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 Graham-Dunnett ♦♦ · Jan 11, 2013 at 12:00 PM 1
Share

I think you'll want start() and update() to have a capital first letter, also.

avatar image andrewmaples Graham-Dunnett ♦♦ · Jan 12, 2013 at 01:28 PM 0
Share

Thanks! I had fixed this shortly after I posted the question, but it would've caused me a headache if I didn't catch it.

avatar image andrewmaples · Jan 12, 2013 at 01:27 PM 0
Share

Thanks! This fixed my issue! I understand now what it means by "use the Start() function to initialize variables." Haha

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

10 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

Related Questions

C# Visual Basic Error 1 Answer

the type or namespace name Note could not be found. are you missing a using directive or an assembly reference. 0 Answers

Using Namespaces? 1 Answer

Cannot assign script to objects when class is within a Namespace 1 Answer

Error CS0246: Type/Namespace Error 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