- Home /
Ambiguous Reference?
I had two of the same scripts (Grenade) and then I named one of them to Grenade one, note that they were not in the same file. Then after I had done that, it gave me another error message. "Assets/Scripts/weapons/Gun.js(269,34): BCE0004: Ambiguous reference 'Grenade': Grenade, Grenade." What does this mean?
var grenadeObj : Grenade = projectile.GetComponent("Grenade") as Grenade;
grenadeObj.soldierCamera = soldierCamera;
Answer by SinisterRainbow · Jun 15, 2013 at 08:32 AM
You had 2 of the same script? Both the class names may still be the same, you can't just change the file name, at least have to change the class name as well if they are the same.
$$anonymous$$aybe, but there is only one class named grenade. It has double of everything in it..? So I tried changing the class Grenade extends $$anonymous$$onoBehaviour
to class Grenade1 extends $$anonymous$$onoBehaviour
, but then it lead to other problems.
If they weren't named the same to begin with, then no need to change names. Did you ever get it working in the first place? Is this for your own game and you're downloading scripts or what? What you posted is really impossible to know much about, i just took a stab with the little information given. :) If you want better help, post as much details as possible.
Yes, both Grenade classes have the same name "class Grenade extends ..." This is why it's saying it's ambiguous. Actually, if you look at the code, they are the exact same, just delete one of the files and it should work, or at least, won't give you that error message anymore.
Answer by zjstudios · Jun 16, 2013 at 04:05 AM
Sorry I'm new haha. I am using an FPS kit from OMA (armedunity.com). I imported the unity demo called Bootcamp and it had two scripts that were the same name. One was HeadLookScript and the other was Grenade. It said these scripts existed in two different locations so what I did was change one of the names to HeadLookScript1, which worked. Then I changed one of the Grenades to Grenade1. That is when a different script called 'Gun' said Ambiguous Reference 'Grenade': Grenade, Grenade. I don't know why. Here are the scripts for you to look at . (Keep in mind the files are the same as the ones in the project + There is only one class for gun and one class for grenade) Grenade:
#pragma strict
#pragma implicit
#pragma downcast
class Grenade extends MonoBehaviour
{
private var thisTransform : Transform;
public var minY : float = -10.0;
public var smoke : GameObject;
public var explosionEmitter : GameObject;
public var explosionTime : float;
public var explosionRadius : float;
public var power : float = 3200;
private var timer : float;
public var soldierCamera : SoldierCamera;
public var nearSounds : AudioClip[];
public var farSounds : AudioClip[];
public var farSoundDistance : float = 25.0;
private var exploded : boolean;
private var hit : RaycastHit;
function Start ()
{
exploded = false;
timer = 0.0;
thisTransform = transform;
}
function Detonate ()
{
if(exploded) return;
exploded = true;
if(renderer != null)
{
renderer.enabled = false;
if(smoke != null)
{
Destroy(smoke);
}
}
else
{
var renderers = GetComponentsInChildren(Renderer);
for(var r : Renderer in renderers)
{
r.enabled = false;
}
}
var _explosionPosition : Vector3 = thisTransform.position;
var col : Collider[] = Physics.OverlapSphere(_explosionPosition, explosionRadius);
var distance : float = Vector3.Distance(soldierCamera.transform.position, _explosionPosition);
soldierCamera.StartShake(distance);
var body : Rigidbody;
if(col != null)
{
for(var c : int = 0; c < col.length; c++)
{
col[c].gameObject.SendMessage("Destruct", SendMessageOptions.DontRequireReceiver);
body = null;
body = col[c].gameObject.rigidbody;
if(body != null)
{
body.isKinematic = false;
}
else if(col[c].gameObject.transform.parent != null)
{
body = col[c].gameObject.transform.parent.rigidbody;
if(body != null)
{
body.isKinematic = false;
}
}
if(body != null)
{
body.AddExplosionForce(power, _explosionPosition, explosionRadius, 3.0f);
}
if(col[c].collider.tag == "glass")
{
col[c].gameObject.SendMessage("BreakAll", SendMessageOptions.DontRequireReceiver);
}
}
}
gameObject.SendMessage("Explode", SendMessageOptions.DontRequireReceiver);
PlaySound(distance);
if(explosionEmitter != null)
{
GameObject.Instantiate(explosionEmitter, transform.position, Quaternion.identity);
}
}
function PlaySound(distance : float)
{
var sIndex : int;
if (distance < farSoundDistance)
{
sIndex = Random.Range(0, nearSounds.length);
audio.PlayOneShot(nearSounds[sIndex]);
timer = nearSounds[sIndex].length + 1.0;
}
else
{
sIndex = Random.Range(0, farSounds.length);
audio.PlayOneShot(farSounds[sIndex]);
timer = farSounds[sIndex].length + 1.0;
}
}
function Update ()
{
if(thisTransform.position.y < minY)
{
Destroy(gameObject);
}
if(exploded)
{
if(timer > 0.0)
{
timer -= Time.deltaTime;
if(timer <= 0.0)
{
Destroy(gameObject);
}
}
}
}
function OnCollisionEnter(c : Collision)
{
if(exploded) return;
Detonate();
}
function OnCollisionStay(c : Collision)
{
if(exploded) return;
Detonate();
}
function OnCollisionExit(c : Collision)
{
if(exploded) return;
Detonate();
}
}
Grenade1:
#pragma strict
#pragma implicit
#pragma downcast
class Grenade extends MonoBehaviour
{
private var thisTransform : Transform;
public var minY : float = -10.0;
public var smoke : GameObject;
public var explosionEmitter : GameObject;
public var explosionTime : float;
public var explosionRadius : float;
public var power : float = 3200;
private var timer : float;
public var nearSounds : AudioClip[];
public var farSounds : AudioClip[];
public var farSoundDistance : float = 25.0;
private var exploded : boolean;
private var hit : RaycastHit;
function Start ()
{
exploded = false;
timer = 0.0;
thisTransform = transform;
}
function Detonate ()
{
if(exploded) return;
exploded = true;
if(renderer != null)
{
renderer.enabled = false;
if(smoke != null)
{
Destroy(smoke);
}
}
else
{
var renderers = GetComponentsInChildren(Renderer);
for(var r : Renderer in renderers)
{
r.enabled = false;
}
}
var _explosionPosition : Vector3 = thisTransform.position;
var col : Collider[] = Physics.OverlapSphere(_explosionPosition, explosionRadius);
var body : Rigidbody;
if(col != null)
{
for(var c : int = 0; c < col.length; c++)
{
col[c].gameObject.SendMessage("Destruct", SendMessageOptions.DontRequireReceiver);
body = null;
body = col[c].gameObject.rigidbody;
if(body != null)
{
body.isKinematic = false;
}
else if(col[c].gameObject.transform.parent != null)
{
body = col[c].gameObject.transform.parent.rigidbody;
if(body != null)
{
body.isKinematic = false;
}
}
if(body != null)
{
body.AddExplosionForce(power, _explosionPosition, explosionRadius, 3.0f);
}
if(col[c].collider.tag == "glass")
{
col[c].gameObject.SendMessage("BreakAll", SendMessageOptions.DontRequireReceiver);
}
}
}
gameObject.SendMessage("Explode", SendMessageOptions.DontRequireReceiver);
if(explosionEmitter != null)
{
GameObject.Instantiate(explosionEmitter, transform.position, Quaternion.identity);
}
}
function PlaySound(distance : float)
{
var sIndex : int;
if (distance < farSoundDistance)
{
sIndex = Random.Range(0, nearSounds.length);
audio.PlayOneShot(nearSounds[sIndex]);
timer = nearSounds[sIndex].length + 1.0;
}
else
{
sIndex = Random.Range(0, farSounds.length);
audio.PlayOneShot(farSounds[sIndex]);
timer = farSounds[sIndex].length + 1.0;
}
}
function Update ()
{
if(thisTransform.position.y < minY)
{
Destroy(gameObject);
}
if(exploded)
{
if(timer > 0.0)
{
timer -= Time.deltaTime;
if(timer <= 0.0)
{
Destroy(gameObject);
}
}
}
}
function OnCollisionEnter(c : Collision)
{
if(exploded) return;
Detonate();
Destroy(gameObject);
}
function OnCollisionStay(c : Collision)
{
if(exploded) return;
Detonate();
Destroy(gameObject);
}
function OnCollisionExit(c : Collision)
{
if(exploded) return;
Detonate();
Destroy(gameObject);
}
}
Gun:
#pragma strict
#pragma implicit
#pragma downcast
enum FireType
{
RAYCAST,
PHYSIC_PROJECTILE,
}
enum FireMode
{
SEMI_AUTO,
FULL_AUTO,
BURST
}
class Gun extends MonoBehaviour
{
public var gunName : String;
public var bulletMark : GameObject;
public var projectilePrefab : GameObject;
public var weaponTransformReference : Transform;
public var hitLayer : LayerMask;
public var woodParticle : GameObject;
public var metalParticle : GameObject;
public var concreteParticle : GameObject;
public var sandParticle : GameObject;
public var waterParticle : GameObject;
//How many shots the gun can take in one second
public var fireRate : float;
public var useGravity : boolean;
private var fireType : FireType;
public var fireMode : FireMode;
//Number of shoots to fire when on burst mode
public var burstRate : int;
//Range of fire in meters
public var fireRange : float;
//Speed of the projectile in m/s
public var projectileSpeed : float;
public var clipSize : int;
public var totalClips : int;
//Time to reload the weapon in seconds
public var reloadTime : float;
public var autoReload : boolean;
public var currentRounds : int;
public var shootVolume : float = 0.4;
public var shootSound : AudioClip;
private var shootSoundSource : AudioSource;
public var reloadSound : AudioClip;
private var reloadSoundSource : AudioSource;
public var outOfAmmoSound : AudioClip;
private var outOfAmmoSoundSource : AudioSource;
private var reloadTimer : float;
@HideInInspector
public var freeToShoot : boolean;
@HideInInspector
public var reloading : boolean;
private var lastShootTime : float;
private var shootDelay : float;
private var cBurst : int;
@HideInInspector
public var fire : boolean;
public var hitParticles : GameObject;
public var shotingEmitter : GunParticles;
private var shottingParticles : Transform;
public var capsuleEmitter : ParticleEmitter[];
public var shotLight : ShotLight;
public var unlimited : boolean = true;
private var timerToCreateDecal : float;
public var pushPower : float = 3.0;
public var soldierCamera : SoldierCamera;
private var cam : Camera;
function OnDisable()
{
if(shotingEmitter != null)
{
shotingEmitter.ChangeState(false);
}
if(capsuleEmitter != null)
{
for(var i : int = 0; i < capsuleEmitter.Length; i++)
{
if (capsuleEmitter[i] != null)
capsuleEmitter[i].emit = false;
}
}
if(shotLight != null)
{
shotLight.enabled = false;
}
}
function OnEnable()
{
cam = soldierCamera.camera;
reloadTimer = 0.0;
reloading = false;
freeToShoot = true;
shootDelay = 1.0 / fireRate;
cBurst = burstRate;
totalClips--;
currentRounds = clipSize;
if(projectilePrefab != null)
{
fireType = FireType.PHYSIC_PROJECTILE;
}
if(shotLight != null)
{
shotLight.enabled = false;
}
shottingParticles = null;
if(shotingEmitter != null)
{
for(var i : int = 0; i < shotingEmitter.transform.childCount; i++)
{
if(shotingEmitter.transform.GetChild(i).name == "bullet_trace")
{
shottingParticles = shotingEmitter.transform.GetChild(i);
break;
}
}
}
}
function ShotTheTarget()
{
if(fire && !reloading)
{
if(currentRounds > 0)
{
if(Time.time > lastShootTime && freeToShoot && cBurst > 0)
{
lastShootTime = Time.time + shootDelay;
switch(fireMode)
{
case FireMode.SEMI_AUTO:
freeToShoot = false;
break;
case FireMode.BURST:
cBurst--;
break;
}
if(capsuleEmitter != null)
{
for(var i : int = 0; i < capsuleEmitter.Length; i++)
{
capsuleEmitter[i].Emit();
}
}
PlayShootSound();
if(shotingEmitter != null)
{
shotingEmitter.ChangeState(true);
}
if(shotLight != null)
{
shotLight.enabled = true;
}
switch(fireType)
{
case FireType.RAYCAST:
TrainingStatistics.shootsFired++;
CheckRaycastHit();
break;
case FireType.PHYSIC_PROJECTILE:
TrainingStatistics.grenadeFired++;
LaunchProjectile();
break;
}
currentRounds--;
if(currentRounds <= 0)
{
Reload();
}
}
}
else if(autoReload && freeToShoot)
{
if(shotingEmitter != null)
{
shotingEmitter.ChangeState(false);
}
if(shotLight != null)
{
shotLight.enabled = false;
}
if(!reloading)
{
Reload();
}
}
}
else
{
if(shotingEmitter != null)
{
shotingEmitter.ChangeState(false);
}
if(shotLight != null)
{
shotLight.enabled = false;
}
}
}
function LaunchProjectile()
{
//Get the launch position (weapon related)
var camRay : Ray = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.6, 0));
var startPosition : Vector3;
if(weaponTransformReference != null)
{
startPosition = weaponTransformReference.position;
}
else
{
startPosition = cam.ScreenToWorldPoint(new Vector3 (Screen.width * 0.5, Screen.height * 0.5, 0.5));
}
var projectile : GameObject = GameObject.Instantiate(projectilePrefab, startPosition, Quaternion.identity);
var grenadeObj : Grenade = projectile.GetComponent("Grenade") as Grenade;
grenadeObj.soldierCamera = soldierCamera;
projectile.transform.rotation = Quaternion.LookRotation(camRay.direction);
var projectileRigidbody : Rigidbody = projectile.rigidbody;
if(projectile.rigidbody == null)
{
projectileRigidbody = projectile.AddComponent("Rigidbody");
}
projectileRigidbody.useGravity = useGravity;
var hit : RaycastHit;
var camRay2 : Ray = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.55, 0));
if(Physics.Raycast(camRay2.origin, camRay2.direction, hit, fireRange, hitLayer))
{
projectileRigidbody.velocity = (hit.point - weaponTransformReference.position).normalized * projectileSpeed;
}
else
{
projectileRigidbody.velocity = (cam.ScreenToWorldPoint(new Vector3(Screen.width * 0.5, Screen.height * 0.55, 40)) - weaponTransformReference.position).normalized * projectileSpeed;
}
}
function CheckRaycastHit()
{
var hit : RaycastHit;
var glassHit : RaycastHit;
var camRay : Ray;
var origin : Vector3;
var glassOrigin : Vector3;
var dir : Vector3;
var glassDir : Vector3;
if(weaponTransformReference == null)
{
camRay = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
origin = camRay.origin;
dir = camRay.direction;
origin += dir * 0.1;
}
else
{
camRay = cam.ScreenPointToRay(new Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
origin = weaponTransformReference.position + (weaponTransformReference.right * 0.2);
if(Physics.Raycast(camRay.origin + camRay.direction * 0.1, camRay.direction, hit, fireRange, hitLayer))
{
dir = (hit.point - origin).normalized;
if(hit.collider.tag == "glass")
{
glassOrigin = hit.point + dir * 0.05;
if(Physics.Raycast(glassOrigin, camRay.direction, glassHit, fireRange - hit.distance, hitLayer))
{
glassDir = glassHit.point - glassOrigin;
}
}
}
else
{
dir = weaponTransformReference.forward;
}
}
if(shottingParticles != null)
{
shottingParticles.rotation = Quaternion.FromToRotation(Vector3.forward, (cam.ScreenToWorldPoint(new Vector3(Screen.width * 0.5, Screen.height * 0.5, cam.farClipPlane)) - weaponTransformReference.position).normalized);
}
if(Physics.Raycast(origin, dir, hit, fireRange, hitLayer))
{
hit.collider.gameObject.SendMessage("Hit", hit, SendMessageOptions.DontRequireReceiver);
GenerateGraphicStuff(hit);
if(hit.collider.tag == "glass")
{
if(Physics.Raycast(glassOrigin, glassDir, glassHit, fireRange - hit.distance, hitLayer))
{
glassHit.collider.gameObject.SendMessage("Hit", glassHit, SendMessageOptions.DontRequireReceiver);
GenerateGraphicStuff(glassHit);
}
}
}
}
function GenerateGraphicStuff(hit : RaycastHit)
{
var hitType : HitType;
var body : Rigidbody = hit.collider.rigidbody;
if(body == null)
{
if(hit.collider.transform.parent != null)
{
body = hit.collider.transform.parent.rigidbody;
}
}
if(body != null)
{
if(body.gameObject.layer != 10 && !body.gameObject.name.ToLower().Contains("door"))
{
body.isKinematic = false;
}
if(!body.isKinematic)
{
var direction : Vector3 = hit.collider.transform.position - weaponTransformReference.position;
body.AddForceAtPosition(direction.normalized * pushPower, hit.point, ForceMode.Impulse);
}
}
var go : GameObject;
var delta : float = -0.02;
var hitUpDir : Vector3 = hit.normal;
var hitPoint : Vector3 = hit.point + hit.normal * delta;
switch(hit.collider.tag)
{
case "wood":
hitType = HitType.WOOD;
go = GameObject.Instantiate(woodParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
case "metal":
hitType = HitType.METAL;
go = GameObject.Instantiate(metalParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
case "car":
hitType = HitType.METAL;
go = GameObject.Instantiate(metalParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
case "concrete":
hitType = HitType.CONCRETE;
go = GameObject.Instantiate(concreteParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
case "dirt":
hitType = HitType.CONCRETE;
go = GameObject.Instantiate(sandParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
case "sand":
hitType = HitType.CONCRETE;
go = GameObject.Instantiate(sandParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
case "water":
go = GameObject.Instantiate(waterParticle, hitPoint, Quaternion.FromToRotation(Vector3.up, hitUpDir)) as GameObject;
break;
default:
return;
}
go.layer = hit.collider.gameObject.layer;
if(hit.collider.renderer == null) return;
if(timerToCreateDecal < 0.0 && hit.collider.tag != "water")
{
go = GameObject.Instantiate(bulletMark, hit.point, Quaternion.FromToRotation(Vector3.forward, -hit.normal));
var bm : BulletMarks = go.GetComponent("BulletMarks");
bm.GenerateDecal(hitType, hit.collider.gameObject);
timerToCreateDecal = 0.02;
}
}
function Update()
{
timerToCreateDecal -= Time.deltaTime;
if(Input.GetButtonDown("Fire1") && currentRounds == 0 && !reloading && freeToShoot)
{
PlayOutOfAmmoSound();
}
if(Input.GetButtonUp("Fire1"))
{
freeToShoot = true;
cBurst = burstRate;
}
HandleReloading();
ShotTheTarget();
}
function HandleReloading()
{
if(Input.GetKeyDown(KeyCode.R) && !reloading)
{
Reload();
}
if(reloading)
{
reloadTimer -= Time.deltaTime;
if(reloadTimer <= 0.0)
{
reloading = false;
if(!unlimited)
{
totalClips--;
}
currentRounds = clipSize;
}
}
}
function Reload()
{
if(totalClips > 0 && currentRounds < clipSize)
{
PlayReloadSound();
reloading = true;
reloadTimer = reloadTime;
}
}
//---------------AUDIO METHODS--------
function PlayOutOfAmmoSound()
{
audio.PlayOneShot(outOfAmmoSound, 1.5);
}
function PlayReloadSound()
{
audio.PlayOneShot(reloadSound, 1.5);
}
function PlayShootSound()
{
audio.PlayOneShot(shootSound);
}
}
Answer by subone · Jan 31, 2014 at 04:00 PM
Sorry I know this is a bit old, but have you found a solution for this ?
I am having the exact same problem. LOL
Your answer
![](https://koobas.hobune.stream/wayback/20220613104214im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Footstep Script Not Working 2 Answers
TERA style movement 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer