Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Its_Faded · May 08, 2020 at 09:02 AM · unity 5unity 2dscript errornamespace

two weird namespace errors

I have like 2 errors from unity that visual studio dont show and I dont know what is means. Please help me I am an idiot.

script I used:

using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Security.Cryptography; using System.Threading; using UnityEngine; public float movement = 5f; public class movement : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { Vector3 movement = new Vector3(Input.getAxis("Horizontal"), 0f, 0f); transform.position += movement * Time.deltaTime*movement; } } void jump() { if (Input.GetButtonDown("Jump")) { gameObject.GetComponent<RigidBody2D>().addForce(new Vector2(0f, 5f), forceMode2D.Impulse); }; } errors from unity:Assets\movement.cs(10,14): error CS0116: A namespace cannot directly contain members such as fields or methods, Assets\movement.cs(28,6): error CS0116: A namespace cannot directly contain members such as fields or methods

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 ADiSiN · May 08, 2020 at 09:47 PM

Hi!

Try next time to format code properly because it will reduce response time since it's hard to understand what is happening in your code. If I didn't make any mistake organizing your code then it looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Security.Cryptography;
 using System.Threading;
 using UnityEngine;
 public float movement = 5f;
 
 public class movement : MonoBehaviour
 { // Start is called before the first frame update 
 
     void Start()
     {
     } // Update is called once per frame void 
     void Update()
     {
         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
         transform.position += movement * Time.deltaTime * movement;
     }
 
     void jump()
     {
         if (Input.GetButtonDown("Jump"))
         {
             gameObject.GetComponent<RigidBody2D>().addForce(new Vector2(0f, 5f), forceMode2D.Impulse);
         };
     }
 }



And there is couple issues with it:

  • Your float movement not inside the class, but above and therefore it throws you an error about that - your variables should be inside your class;

  • Your instantiated variable shouldn't be named same as your class;

  • Pay attention to characters case, because RigidBody2D will throw an error, but Rigidbody2D will not, because programming language is case sensitive;

  • You are using a lot of libraries (using, using, ...) that you are not implementing, but I won't remove them, because maybe you will need them later, just keep in mind that grayed out using field means you are not implementing library that called.


The code below won't throw any errors.

 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Security.Cryptography;
 using System.Threading;
 using UnityEngine;
 
 public class movement : MonoBehaviour
 {
     public float f_movement = 5f;
 
     void Update()
     {
         Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
         transform.position += movement * Time.deltaTime * f_movement;
     }
 
     void jump()
     {
         if (Input.GetButtonDown("Jump"))
         {
             GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
         }
     }
 }


Hope that helps.

Comment
Add comment · 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 Its_Faded · May 09, 2020 at 07:20 AM 0
Share

Almost everything is right, except for jump script section. Jump is not working. Fix it pls beacuse I dont know how to write scripts.

avatar image KapoorArjun19 Its_Faded · May 09, 2020 at 08:19 AM 0
Share

Jump doesn't work because jump() never gets called. Refer to this for more info: https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html

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

229 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

Related Questions

Unity Teams Errors When Transferring 0 Answers

How to make the tower shoot at the one who is closer to the finish line? 0 Answers

[Help!]Unity FPS Health script error. 1 Answer

The type or namespace "UnityEngine" could not be found - Can not edit scripts 2 Answers

Unexpected symbol '(' in class, or interface member declaration 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