- Home /
I'm having trouble understanding what "does not contain a definition for" means.
So inside of my player controller I've have a way to take my current gun and replace it with a new gun (via buy or drop or whatever, thats for later).
void EquipGun(int i){
if (currentGun) {
Destroy(currentGun.gameObject);
}
currentGun = Instantiate (guns [i], handHold.position, handHold.rotation) as Gun;
currentGun.transform.parent = handHold;
handHold is my variable for where every gun will be on characters, but instantiating this line with my Gun class is where I get the error because I added the position and rotation for handHold. Here is my Gun class:
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class Gun : MonoBehaviour {
public enum GunType {Semi,Burst,Auto};
public LayerMask collisionMask;
public float gunID;
public GunType gunType;
public float rpm;
public float damage = 1;
public Transform spawn;
public Transform shellEjectionPoint;
public Rigidbody shell;
private LineRenderer tracer;
//System:
private float secondsBetweenShots;
private float nextPossibleShootTime;
void Start(){
secondsBetweenShots = 60 / rpm;
if (GetComponent<LineRenderer>()) {
tracer = GetComponent<LineRenderer>();
}
}
public void Shoot(){
if (CanShoot ()) {
Ray ray = new Ray (spawn.position, spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast (ray,out hit, shotDistance, collisionMask)){
shotDistance = hit.distance;
if (hit.collider.GetComponent<Enitity>()){
hit.collider.GetComponent<Enitity>().TakeDamage(damage);
}
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
GetComponent<AudioSource>().Play();
if (tracer) {
StartCoroutine ("RenderTracer", ray.direction * shotDistance);
}
Rigidbody newShell = Instantiate(shell, shellEjectionPoint.position,Quaternion.identity) as Rigidbody;
newShell.AddForce (shellEjectionPoint.forward * Random.Range (150f,200f) + spawn.forward * Random.Range(-10f,10f));
}
}
public void ShootContinuous(){
if (gunType == GunType.Auto) {
Shoot();
}
}
private bool CanShoot(){
bool canShoot = true;
if (Time.time < nextPossibleShootTime){
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint){
tracer.enabled = true;
tracer.SetPosition (0,spawn.position);
tracer.SetPosition (1,spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}
Okay sorry, here are my errors, there's six of them.
1 Assets/Scripts/PlayerController.cs(64,62): error CS1061: Type Gun' does not contain a definition for
position' and no extension method position' of type
Gun' could be found (are you missing a using directive or an assembly reference?)
2 Assets/Scripts/PlayerController.cs(64,81): error CS1061: Type Gun' does not contain a definition for
rotation' and no extension method rotation' of type
Gun' could be found (are you missing a using directive or an assembly reference?)
3 Assets/Scripts/PlayerController.cs(64,30): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments 4 Assets/Scripts/PlayerController.cs(64,30): error CS1503: Argument
#2' cannot convert object' expression to type
UnityEngine.Vector3'
5 Assets/Scripts/PlayerController.cs(65,38): error CS0029: Cannot implicitly convert type Gun' to
UnityEngine.Transform'
6 Assets/Scripts/PlayerController.cs(66,26): error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'
Thank you, but my gun does have a transform, I have an object "weapon" it's the only thing the Gun script is attached to see:
And thank you for the fix for 4, I put in a quick fix for errors 1, 2, and 3 while I try to figure them out, I still have 1 error left (or rather still 4), error number 6 that comes from my last line with the Animator, I think I can solve that one but I'm trying to understand why Gun have no transform when the object it's attached to does have one.
happens to me when I forget to save the script and unity only refreshed a referencing one.
But yeah, the full error and the error line would be nice
There's six error codes
1.Assets/Scripts/PlayerController.cs(64,81): error CS1061: Type Gun' does not contain a definition for
rotation' and no extension method rotation' of type
Gun' could be found (are you missing a using directive or an assembly reference?)
2.Assets/Scripts/PlayerController.cs(64,62): error CS1061: Type Gun' does not contain a definition for
position' and no extension method position' of type
Gun' could be found (are you missing a using directive or an assembly reference?)
3.Assets/Scripts/PlayerController.cs(64,30): error CS1502: The best overloaded method match for UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments 4.Assets/Scripts/PlayerController.cs(64,30): error CS1503: Argument
#2' cannot convert object' expression to type
UnityEngine.Vector3'
5.Assets/Scripts/PlayerController.cs(65,38): error CS0029: Cannot implicitly convert type Gun' to
UnityEngine.Transform'
6.Assets/Scripts/PlayerController.cs(66,26): error CS0120: An object reference is required to access non-static member `UnityEngine.Animator.SetFloat(string, float)'
Answer by Adriansurf · Jul 07, 2015 at 06:36 AM
The problem is in PlayerController.cs file. Without it it's hard to fix all errors.
Class Gun is type of MonoBehaviour not Transform so it don't have position and rotation fields out of the box. You have to get this values through transform: change handHold.position to handHold.transform.position. This should fix issue 1, 2, 3.
currentGun.transform.parent = handHold; change to currentGun.transform.parent = handHold.transform; - this will fix 4
Okay, I solved problems 1-3. I accidentally made public Gun handHold; ins$$anonymous$$d of public Transform handHold. lol
Answer by Eric5h5 · Jul 06, 2015 at 11:48 PM
Just what it says: your Gun class has no definition for "position". You have defined "collisionMask", "gunID", "gunType", and so on, but there's no definition for "position". It sounds like you're trying to access the Transform class, which does have definitions for position and rotation. You can't convert from Transform to Gun.
Answer by sourav13 · Jul 07, 2015 at 07:27 AM
Yes you got to take game object gun I mean on which your gun script is there and than take its transform.