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)”. This fixed some errors, but now I cannot add the script as a component, and I have no idea how to fix this 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);
    	}	
    }

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 things like that happen to me in the past…lol

:slight_smile:

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.

Thank you all for the time and support you put into helping me with my issue. I didn’t know which was the class name and which was the file name, but after the all of this 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.