• 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 epicninjabomber · Apr 11, 2015 at 10:07 AM · scripting problem

Can't add script ass a component to a gameobject.

I am following a really intriguing Unity tutorial series, and we have gotten to a point where we begin coding. I have virtually no experience with using Unity, other than the few hours I have put into the tutorial as well as messing around a bit, also I have no experience with writing code at all let alone C#. He is using Unity 4.5.0 in the series (I believe) and he uses code lines like "rigidbody.AddForce(0, jumpspeed, 0)", but when I looked up the Unity 5 translation, it states: "GetComponent().AddForce (0, jumpspeed, 0)". T$$anonymous$$s fixed some errors, but now I cannot add the script as a component, and I have no idea how to fix t$$anonymous$$s issue. As I said I have no experience with the syntax of C#, I greatly appreciate your support and assistance. Thank you kindly.

Here is my script:

  using UnityEngine;
  using System.Collections;
     
     public class BouncingBallScript : MonoBehaviour {
     
         public float jumpspeed=10;
     
         void Update ()
         {
             if(Input.GetKeyDown("Jump"))
             GetComponent<Rigidbody>().AddForce (0, jumpspeed, 0);
         }    
     }





Comment

People who like this

0 Show 3
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 gjf · Apr 10, 2015 at 12:39 PM 0
Share

are there any more errors? if so, please post them. unity won't allow you to add components while there are any errors.

also, it might be an idea to post a link to the tutorial that you're following - there may be issues in the original...

avatar image epicninjabomber · Apr 11, 2015 at 06:56 PM 0
Share

Thank you both for your responses. I took your suggestion Phantom, but I still get the same error message. I restarted multiple times, still with no luck. I continue to get the same error message: "Can't add script- Can't add script component 'BouncingBall' because the script class cannot be found. Make sure the file name and the class name match." To answer your question gjf, no I do not have any other errors, in monodevelop or in the console. I think it may have something to do with the file and class name not matching but I'm not exactly sure which those are. Thank you again!

avatar image Bonfire-Boy · Apr 11, 2015 at 07:04 PM 0
Share

The class name is "BouncingBallScript", because the class is declared with the line

public class BouncingBallScript : MonoBehaviour

The file this code is in needs to be called BouncingBallScript.cs, because in order for them to work in Unity, MonoBehaviours classes have to be in files which are exactly the same (including case) as the class name, plus the .cs extension.

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Digital-Phantom · Apr 11, 2015 at 10:16 AM

I get no errors when adding your Update function to an existing script?

As gjf said, are you getting any other error messages?

Also I know it sounds silly, but just try saving your project, then close and re-open Unity, had strange t$$anonymous$$ngs like that happen to me in the past....lol

:)

Comment
Jiraiyah
rob5300
epicninjabomber

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 Xarbrough · Apr 11, 2015 at 07:00 PM

It sounds like you answered your own question, but just to make it clear: In Unity all MonoBehaviour classes must have the same file name as your class name. So if your class is "BouncingBallScript", you will have to rename the script file in the Project view in Unity to "BouncingBallScript" as well. Or call both "BouncingBall".If you rename one of the two, you have to rename both and save.

Comment

People who like this

0 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 epicninjabomber · Apr 12, 2015 at 09:38 PM

Thank you all for the time and support you put into helping me with my issue. I didn't know w$$anonymous$$ch was the class name and w$$anonymous$$ch was the file name, but after the all of t$$anonymous$$s it was my simple mistake of having my file name "BouncingBall" and my class name as "BouncingBallScript". Thank you all once again, I now understand truly how wonderful the Unity Community really is. I look forward to seeing one of your all's games in the future. Have a wonderful day, and good luck with all your games, and happy coding.

Comment

People who like this

0 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Make ScriptableObject assets customizable. 1 Answer

How to disable a script on a bunch of instantiated objects with tag ? 1 Answer

How to remove duplicate values from a list 1 Answer

Help required for shutting down compiled App 0 Answers

How do I tie my movement script to only selected objects?,How do I tie my movement script to the my selection script? 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