Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

C# NullReferenceException Error

$
0
0
I cannot figure out what is wrong with this but Unity keeps giving me a NullReferenceException: Object reference not set to an instance of an object for this on two lines of this. this is the method where the errors occur; void WeaponControl (Transform currentGun, string gunName, int gunDamage) { Transform otherMuzzleFlash; Transform otherBloodHole; Transform otherGunHole; if(Input.GetButtonDown("Fire1")&&!currentGun.animation.isPlaying) { otherMuzzleFlash = Instantiate(muzzleFlash, muzzle.position, Quaternion.identity) as Transform; otherMuzzleFlash.transform.parent = transform; currentGun.audio.Play(); currentGun.animation.Play(gunName + "Recoil"); RaycastHit hit; if(Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit)) { Debug.Log("Raycasted"); Debug.DrawLine(transform.position, hit.point); if(hit.transform.tag != "Enemy") { otherGunHole = Instantiate(gunHole, hit.point + (hit.normal * 0.001f), Quaternion.LookRotation(hit.normal)) as Transform; otherGunHole.parent = hit.transform; //Error here } else { hit.transform.audio.Play(); otherBloodHole = Instantiate(bloodHole, hit.point + (hit.normal * 0.001f), Quaternion.LookRotation(hit.normal)) as Transform; otherBloodHole.parent = hit.transform; //Error here hit.transform.SendMessage("ApplyDamage", gunDamage * damageMultiplier, SendMessageOptions.DontRequireReceiver); carnage += 1; } } } }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>