why my second class is not working?

I have a script attached to the player and that script has 2 classes attached to it. 1 for shooting and the other for moving. For some reason the shooting class i made is not working? Have i done something wrong or is it not possible to have to classes in 1 script?

public class PlayerShip : MonoBehaviour {

	public float speed = 5;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		float h = Input.GetAxis ("Horizontal");
		float v = Input.GetAxis ("Vertical");



		transform.Translate (0, 0, v * speed * Time.deltaTime);
		transform.Rotate (0, h * 100 * Time.deltaTime,0);
	}
}

public class Combat : Projectile{

	public void Update(){
		if(Input.GetKey (KeyCode.Space)){
			Debug.DrawRay(transform.position,Vector3.forward);
			print ("Shooting");
		}
	}
}

you can’t have two classes in a single file like that. you can make the second class a Nested class or making a new file for the second class.

Nested Class

this is how you make a nested class. just remember is scope is solely within Playership, meaning doesn’t exist anywhere else. this is useful for organizing variable in the editor, having multiple “sets” of variables. you can have may nested classes within the main class. this is an example, it may not compile due to it inheriting to “Projectile”. but you get the idea of what it is about.

public class PlayerShip : MonoBehaviour {
 
    public float speed = 5;
 public class Combat : Projectile{
 
    public void Update(){
       if(Input.GetKey (KeyCode.Space)){
         Debug.DrawRay(transform.position,Vector3.forward);
         print ("Shooting");
       }
    }
}
    // Use this for initialization
    void Start () {
 
    }
 
    // Update is called once per frame
    void Update () {
 
       float h = Input.GetAxis ("Horizontal");
       float v = Input.GetAxis ("Vertical");
 
 
 
       transform.Translate (0, 0, v * speed * Time.deltaTime);
       transform.Rotate (0, h * 100 * Time.deltaTime,0);
    }
}

New File

just copy the second class to a new file. i recommend this option since its cleaner, more organized and just makes more common sense for what you are doing. you probably know how to figure this out by now but just in case.

  1. In unity Create a new C# script, name it Combat.

  2. open the new script, Cut and paste it in the new file

  3. Save.

your script should compile now and be cozy in its new file. note that you can have many Nested classes. the reason you can’t have to classes parallel like that is because the way unity compiles it scripts with monobehaviour and what not.

hope it helps.