'Coroutine is resetting material and changing rotation
I have a really weird bug that I can't pinpoint. I have 2 weapons - an assault rifle and a burst rifle. I use a coroutine for the BR, but it removes the bullet's material and fires from the wrong angle.
AR video Full AR code: https://pastebin.com/eHNCQ4Yf
void Shoot()
{
GameObject BulletNew = Instantiate(Bullet, firePoint.position, firePoint.rotation);
BulletNew.GetComponent<Bullet>().sender = gameObject.transform.root.gameObject;
Destroy(BulletNew, 2);
}
BR video Full BR code: https://pastebin.com/wqqdrPbv
void Shoot()
{
GameObject BulletNew = Instantiate(Bullet, firePoint.position, firePoint.rotation);
BulletNew.GetComponent<Bullet>().sender = gameObject.transform.root.gameObject;
Destroy(BulletNew, 2);
}
public IEnumerator FireBurst()
{
for (int i = 0; i < burstSize; i++)
{
GetComponent<soundManager>().PlaySound();
yield return new WaitForSeconds(bulletDelay);
Shoot();
}
}
However, when I unparent the weapon it works perfectly.
Both use the same references, I've triple checked. When I comment out the coroutine for the BR and just use the regular shoot function it's completely normal. I rid of recoil, same result. Any ideas about this?
Solution 1:[1]
Your code isn't getting the gun, it's getting the character:
BulletNew.GetComponent<Bullet>().sender = gameObject.transform.root.gameObject;
I don't know what Bullet.sender does here, but I would guess you're getting the transform.forward or something, but you're not passing the gameObject, which I'm assuming would be the gun itself, but instead you're passing gameObject.transform.root.gameObject, which is the root object in the project hierarchy, which is why it works when you detach the gun - in that case the gun IS the root object.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chuck |
