I am having trouble making a reference.

Hi I am a starter but I have the basics of coding in Unity but I don’t understand why I cant make a reference can some one help me, Please!

Here is the script: But I cant make a reference the weapon (glock)

using UnityEngine;

[System.Serializable]
public class playerWeapon : MonoBehaviour
{
    public string name = "Glock";

    public float damage = 10f;
    public float Range = 100f;

} 

Thanks for your help

I am not entirely sure I get what you mean. Creating a reference works the same in Unity as it does in C# in general. You have a class and you create an instance and store it in a variable. That variable is a reference to that object in memory.

So for your playerWeapon class you would use the default constructor like:

var glock = new playerWeapon();

and then you would reference the glock variable when you wanted to access or set its members i.e. glock.damage = 32.0f;;


A few things. C# classes should be pascal case so should most class members. Go and have a look at the c# style guide or just pay attention to how the .NET framework is written.

Oohhh now I get it, Thanks!