Silly question about using methods from different classes (C#)

So I’m working on my very first attempt at a game in Unity and I have a little problem.was hoping you could help.

I’m creating a little space shooters and I’ve decided that I want all my weapon firing functions to be in a different file with a separate class. Each weapon will have its own method and this way everything will be much more organised than it was before.

So I created a new file in Visual Studio, created a new class called Weapons, made it a subclass of Monobehaviour and set up a method called “public void Lasers(Vector3 PositionofShooter, GameObject LaserGameObject, int LevelofWeapon)”.

(As you can see I’ve decided to keep my GameObject for lasers inside the player code since “Weapons” will not be a attached to any object anyway… (Can I even do that? use a code not attached to an object?)

Now for my problem. Inside “Player”, how do I use the method “Lasers”? When I type in “weapons.” it suggest some methods that belong to monobehaviour but not my custom method. What the hell am I doing wrong here?

Nope. My bad, I seem to be unable to access “Weapons” at all… Is it because it is in a different file? How do I fix that?

If you make Lasers a public static method then it will be accessible without an instance of Weapons which, from what it sounds like anyway, is what you’re trying to accomplish.

If you set it only to public variable you can use call GetComponent and access it. I would advise against static vars unless it requires only 1 instance of it. Like player’s health, bullets, name. Those variables can only have 1 instance.

Well zine there is a cool little trick you can do that allows you to use the entire script just as the GetComponent does.

public class ObjectInfo: MonoBehaviour 
{
	public static ObjectInfo Instance = null;
}

void Start () 
	{
		Instance = this;
        }

Then whenever you want the access this ObjectInfo Class all you do is call that Instance.

ObjectInfo.Instance.MyMethod(), Or you can even access variables in that class aslong as they are public.