- Home /
Never answered, but answer not needed anymore.
Ammo script "Does not Denote a valid Type" error
i have tried everything, i looked up scripting help, tutorials and cant fix this.
enum PickupType { Health = 0, Rocket = 1, Ammo = 20, Shells = 2, Clips = 20, AlienArtifact01, }
var pickupType = PickupType.Health;
var amount = 20;
var sound : AudioClip;
private var used = false;
function ApplyPickup (Player : Player) {
if (pickupType == PickupType.Health) {
if (Player.PlayerHealth >= Player.maximumPlayerHealth)
return false;
Player.PlayerHealth += amount;
Player.PlayerHealth = Mathf.Min(Player.PlayerHealth, Player.maximumPlayerHealth);
}
else if (pickupType == PickupType.Rocket) {
var launcher : RocketLauncher = Player.GetComponentInChildren(RocketLauncher);
if (launcher)
launcher.ammoCount += amount;
}
else if (pickupType == PickupType.Ammo) {
var ammo : MachineGun = Player.GetComponentInChildren(MachineGun);
if (ammo)
ammo.bulletsPerClip += amount;
}
else if (pickupType == PickupType.Shells) {
var Shells : Shotgun = Player.GetComponentInChildren(Shotgun);
if (Shells)
Shells.bulletsPerClip += amount;
}
else if (pickupType == PickupType.Clips) {
var Clips : Pistol = Player.GetComponentInChildren(Pistol);
if (Clips)
Clips.grenadesLeft += amount;
}
return true;
}
function OnTriggerEnter (col : Collider) {
var Player : Player = col.GetComponent(Player);
//* Make sure we are running into a Player
//* prevent picking up the trigger twice, because destruction
// might be delayed until the animation has finnished
if (used || Player == null)
return;
if (!ApplyPickup (Player))
return;
used = true;
// Play sound
if (sound)
AudioSource.PlayClipAtPoint(sound, transform.position);
// If there is an animation attached.
// Play it.
if (animation && animation.clip) {
animation.Play();
Destroy(gameObject, animation.clip.length);
} else {
Destroy(gameObject);
}
}
// Auto setup the pickup
function Reset () {
if (collider == null)
gameObject.AddComponent(BoxCollider);
collider.isTrigger = true;
}
it says that the values of RocketLaucher MachineGun Shotgun Player Pistol do not denote a valid type. what am i doing wrong, or am i over complicating it -_-
is RocketLaucher $$anonymous$$achineGun Shotgun Player Pistol the names of the scripts for each of the weapon types, meaning you either defined a class called each of those or there are files called, RocketLaucher.js, $$anonymous$$achineGun.js, Shotgun.js, Player.js, Pistol.js.
Have you tried using the generic version of GetComponentInChildren?
function ApplyPickup (Player : Player) {
if (pickupType == PickupType.Health) {
if (Player.PlayerHealth >= Player.maximumPlayerHealth)
return false;
Player.PlayerHealth += amount;
Player.PlayerHealth = $$anonymous$$athf.$$anonymous$$in(Player.PlayerHealth, Player.maximumPlayerHealth);
}
else if (pickupType == PickupType.Rocket) {
var launcher : RocketLauncher = Player.GetComponentInChildren.<RocketLauncher>();
if (launcher)
launcher.ammoCount += amount;
}
else if (pickupType == PickupType.Ammo) {
var ammo : $$anonymous$$achineGun = Player.GetComponentInChildren.<$$anonymous$$achineGun>();
if (ammo)
ammo.bulletsPerClip += amount;
}
else if (pickupType == PickupType.Shells) {
var Shells : Shotgun = Player.GetComponentInChildren.<Shotgun>();
if (Shells)
Shells.bulletsPerClip += amount;
}
else if (pickupType == PickupType.Clips) {
var Clips : Pistol = Player.GetComponentInChildren.<Pistol>();
if (Clips)
Clips.grenadesLeft += amount;
}
return true;
}
To start with, this is wrong:
function ApplyPickup (Player : Player)
You shouldn't have variables named the same thing as a type. Only use lowercase for variables.
ok, i tried what you suggested but i dont know what to put after the 'var Clips : Pistol' part. i simplified the script to only one type of ammo, and nothing else, but it still doesnt work. heres the current script:
enum PickupType { Health = 0, Rocket = 1,}
var pickupType = PickupType.Health;
var amount = 20;
var sound : AudioClip;
private var used = false;
function ApplyPickup (player : Player) {
if (pickupType == PickupType.Rocket) {
var launcher : RocketGun = player.GetComponentInChildren("RocketLauncher").Gun;
if (launcher)
launcher.ammoCount += amount;
}
return true;
}
function OnTriggerEnter (col : Collider) {
var player : Player = col.GetComponent(Player);
//* $$anonymous$$ake sure we are running into a Player
//* prevent picking up the trigger twice, because destruction
// might be delayed until the animation has finnished
if (used || player == null)
return;
if (!ApplyPickup (player))
return;
used = true;
// Play sound
if (sound)
AudioSource.PlayClipAtPoint(sound, transform.position);
// If there is an animation attached.
// Play it.
{
Destroy(gameObject);
}
}
// Auto setup the pickup
function Reset () {
if (collider == null)
gameObject.AddComponent(BoxCollider);
collider.isTrigger = true;
}
Have you tried setting a break point in $$anonymous$$onoDevelop and attaching to the unity process to see what is actually going on with your functions and variables?