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 *, 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: TypeGun' does not contain a definition for
position’ and no extension methodposition' 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: TypeGun' does not contain a definition for
rotation’ and no extension methodrotation' 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 forUnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments* *4 Assets/Scripts/PlayerController.cs(64,30): error CS1503: Argument
#2’ cannot convertobject' expression to type
UnityEngine.Vector3’
5 Assets/Scripts/PlayerController.cs(65,38): error CS0029: Cannot implicitly convert typeGun' 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:
[49659-capture.jpg|49659]*
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.