My script doesnt work?
Hi
Im trying to add another weapon to my weapon script, but i just cant get it to work without errors.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class PlayerWeapon {
namespace Primary
public string name = "Rifle";
public float damage = 10f;
public float range = 200f;
public float clip = 20f;
public float fireRate = 4f;
public GameObject GFX;
namespace Secondary
public string name = "Pistol";
public float damage = 40f;
public float range = 50f;
public float clip = 7f;
public float fireRate = 10f;
}
this script contains weapon data.
What's that namespace Primary there? if you want this class to be in that namespace you have to either put it in that file or create a partial namespace. That line is not closed with anything so it will give errors. https://unity3d.com/learn/tutorials/modules/intermediate/scripting/namespaces
thanks now im using classes but my other script PlayerShoot.cs loads up with errors such as that it cant find the float that i referenced in the other script.
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (Weapon$$anonymous$$ananger))]
public class PlayerShoot : $$anonymous$$onoBehaviour {
[SerializeField]
private Camera cam;
[SerializeField]
private Layer$$anonymous$$ask mask;
private Weapon$$anonymous$$ananger weapon$$anonymous$$ananger;
private PlayerWeapon currentWeapon;
void Start ()
{
if (cam == null)
{
Debug.LogError ("PlayerShoot: NO CA$$anonymous$$ERA REFERENCED!!!");
this.enabled = false;
}
weapon$$anonymous$$ananger = GetComponent<Weapon$$anonymous$$ananger> ();
}
void Update () {
currentWeapon = weapon$$anonymous$$ananger.GetCurrentWeapon ();
if (currentWeapon.fireRate <= 0f)
{
if (Input.GetButtonDown ("Fire1")) {
Shoot();
}
} else
{
if (Input.GetButtonDown ("Fire1")) {
InvokeRepeating ("Shoot", 0f, 1f / currentWeapon.fireRate);
} else if (Input.GetButtonUp ("Fire1")) {
CancelInvoke ("Shoot");
}
}
}
void Shoot () {
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, currentWeapon.range, mask))
{
Debug.Log("Player just shoot at " + _hit.collider.name);
}
}
}
I'm not sure what you mean. A float is a value type not a reference type so you can't reference it. Please copy and paste some of those errors.
Assets/Entities/Weapon/PlayerShoot.cs(37,35): error CS1061: Type PlayerWeapon' does not contain a definition for
fireRate' and no extension method fireRate' of type
PlayerWeapon' could be found (are you missing a using directive or an assembly reference?) Assets/Entities/Weapon/PlayerShoot.cs(45,82): error CS1061: Type PlayerWeapon' does not contain a definition for
fireRate' and no extension method fireRate' of type
PlayerWeapon' could be found (are you missing a using directive or an assembly reference?) Assets/Entities/Weapon/PlayerShoot.cs(45,33): error CS1502: The best overloaded method match for UnityEngine.$$anonymous$$onoBehaviour.InvokeRepeating(string, float, float)' has some invalid arguments Assets/Entities/Weapon/PlayerShoot.cs(45,33): error CS1503: Argument
#3' cannot convert object' expression to type
float' Assets/Entities/Weapon/PlayerShoot.cs(54,108): error CS1061: Type PlayerWeapon' does not contain a definition for
range' and no extension method range' of type
PlayerWeapon' could be found (are you missing a using directive or an assembly reference?) Assets/Entities/Weapon/PlayerShoot.cs(54,29): error CS1502: The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Vector3, UnityEngine.Vector3, out UnityEngine.RaycastHit, float, int)' has some invalid arguments Assets/Entities/Weapon/PlayerShoot.cs(54,29): error CS1503: Argument
#4' cannot convert object' expression to type
float'