0

I got 2 same classes, one for the primary weapon and the other for the secondary one. Here are my classes :

public class weapon : MonoBehaviour {

public string Name { get; set; }
public float Damages { get; set; }
public float FireRate { get; set; }
public float Range { get; set; }
public float BulletSpeed { get; set; }
public bool isOn { get; set; }

public weapon(string name, float damages, float fireRate, float range, float bulletSpeed, bool ison) {

    this.Name = name;
    this.Damages = damages;
    this.FireRate = fireRate;
    this.Range = range;
    this.BulletSpeed = bulletSpeed;
    this.isOn = ison;
    }
}

public class weapon1 : MonoBehaviour {

public string Name { get; set; }
public float Damages { get; set; }
public float FireRate { get; set; }
public float Range { get; set; }
public float BulletSpeed { get; set; }
public bool isOn { get; set; }

public weapon1(string name, float damages, float fireRate, float range, float bulletSpeed, bool ison) {

    this.Name = name;
    this.Damages = damages;
    this.FireRate = fireRate;
    this.Range = range;
    this.BulletSpeed = bulletSpeed;
    this.isOn = ison;
    }
}

I want in another script that when I press 'F', the weapons take respectively their specs, but it gets stuck at giving the name to the second one, here is my script to give them their specs:

    void Update () {

    if (Input.GetKeyDown (KeyCode.F)) {

        GetComponent<weapon> ().Name = "Rien";
        GetComponent<weapon> ().Damages = 0;
        GetComponent<weapon> ().FireRate = 0;
        GetComponent<weapon> ().Range = 0;
        GetComponent<weapon> ().BulletSpeed = 0;
        GetComponent<weapon> ().isOn = true;

        Debug.Log (GetComponent<weapon> ().Name);

        GetComponent<weapon1> ().Name = "Rien1"; //stuck here... :'(
        GetComponent<weapon1> ().Damages = 0;
        GetComponent<weapon1> ().FireRate = 0;
        GetComponent<weapon1> ().Range = 0;
        GetComponent<weapon1> ().BulletSpeed = 0;
        GetComponent<weapon1> ().isOn = false;

        Debug.Log(GetComponent<weapon1> ().Name);
    }

}

I'm getting 'Object reference not set to an instance of an object' and I did exactly the same thing for both weapons Thanks in advance

Yooooomi
  • 895
  • 1
  • 8
  • 20
  • 3
    You shouldn't make multiple classes that are exactly the same like this. Make one class and then instantiate it as many times as you need and change its members to make each instance unique. – Danny Herbert May 08 '16 at 11:27
  • 1
    Well, then `GetComponent()` returned `null`. Are you sure both a `weapon` and `weapon1` script are on the SAME object? Also: I find it to be bad style to have two classes with exactly the same code but just a different name. It'd be painfull to make changes to them and maintain them. Just do one "Weapon" script and add another property to distinguish between primary and secondary weapon. – Maximilian Gerhardt May 08 '16 at 11:28
  • Do you have an idea of how i should do ? – Yooooomi May 08 '16 at 11:28
  • 1
    If in future you want to make one of the classes different then you should use inheritance here and have a base class of weapon and then a derived class for primary and secondary weapons that inherit from the weapon class. – Danny Herbert May 08 '16 at 11:29
  • Ok but how can i Instantiate two different Objects ? – Yooooomi May 08 '16 at 11:30
  • Ok nice i managed to do it :D, thanks guys, can someone 'answer' my question so i can mark it as solved – Yooooomi May 08 '16 at 11:38

1 Answers1

1

You are making some basic programming errors as well as some basic Unity specific errors. As said in the comments, you shouldn't have lots of classes that are exactly the same. Remember to not repeat your self.

Then you are inheriting from MonoBehaviour but are defining a constructor method to set your fields. Don't do this in unity. Make the feilds available in the inspector by making them public or adding the [SerializeField] attribute to private member variables. If you have anything you want to do when the object is created then put this in the Start() method:

public Weapon : MonoBehviour
{
    public string name; //set these in the inspector for each weapon
    public float damage;
    //etc

    // in Unity you use the Start method for mono behaviours. NOT the constructor method
    void Start()
    {
        //Do things when the object is created
    }
}

Now, it's not clear how you are adding weapons to your player from your question but what you should do is add a game object as a child to the player object in the hierarchy. Do this for each weapon and then add your weapon script to each weapon. Then change the fields that you made available to yourself in the inspector. You can then get a reference to the weapons that the player currently has via a script on the player and do as you please with their values when you press a key.

Danny Herbert
  • 2,002
  • 1
  • 18
  • 26
  • 1
    I totally rewrote my script letting only one class and i'll take note of what you said thank you very much :D – Yooooomi May 08 '16 at 11:46
  • Very nice, glad to hear. If you are just starting out with unity then check out the [official tutorials](https://unity3d.com/learn/tutorials/topics/scripting) . They are very clear with the basics! best of luck. – Danny Herbert May 08 '16 at 11:48