RROR - An object reference is required to access non-static member, how do i fix?
Ive seen several of these posts yet i cant understand what to do and also i get "playerweapon dosent contain a constructor that takes 5 arguments".
using UnityEngine;
[System.Serializable]
//public class PlayerWeapon {
// public string name = "Blaster";
// public int damage = 10; // public float range = 100f;
// public float fireRate = 0f;
// public GameObject graphics; //}
public class PlayerWeapon { // Field public string name; public int damage; public float range; public float fireRate; public static GameObject graphics;
// Constructor that takes no arguments.
public PlayerWeapon()
{
name = "Blaster";
damage = 10;
range = 100f;
fireRate = 0f;
GameObject graphics;
}
// Constructor that takes one argument.
public PlayerWeapon(string _name, int _dmg, float _range, float _fireRate, GameObject _graphics) //GameObject _graphics
{
name = _name;
damage = _dmg;
range = _range;
fireRate = _fireRate;
graphics = _graphics;
}
class Weapon
{
public static void Main(string[] args)
{
Weapon Rifle = new Weapon("Rifle", 20, 200f, 0f, Riflegraphs);
}
}
}
Theres a few things I noticed that probably dont need to be there, because Unity is object-oriented scripting, you dont need to do stuff like "main", the script should exist on an object, and the whole "class" becomes "main" itself, in a way
Regardless, when you create another class, to access things in it, you need to initialize it as a regular "variable" first.
So for example: You have PlayerWeapon as a class, before you can say "PlayerWeapon."... You need something like: PlayerWeapon pWep = new PlayerWeapon(); Or even simply just: PlayerWeapon pWep;
And now "pWep."... is possible - so you use the "reference" of your class PlayerWeapon under a variable ins$$anonymous$$d of directly accessing the class itself.
Hope that helps.