This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
1
Instantiating, or coroutine is causing my entire unity to crash.
Post Flair (click to view more posts with a particular flair)
Post Body
SOLVED
Im working on a project im doing for a theater, and we have an animation that plays after the shoot cooldown is over, however after it instantiates the object it freezes my entire unity, this never happened before i made the code to incorporate the new coroutine and animation, so idk exactly what is causing it.
using System.Collections;
using UnityEngine;
public class CerberusHead : MonoBehaviour
{
public float throwForce = 10f;
public float projectileSpeed = 5f; // Adjust the speed as needed
public Transform highSpawnPoint; // Spawn point for high projectiles
public Transform lowSpawnPoint; // Spawn point for low projectiles
public Transform backWall;
public GameObject cerberusHead;
public float attackTimer = 7f;
public float attackTimerWithAnimation = 14f;
public SpriteRenderer topHeadRenderer;
public SpriteRenderer bottomHeadRender;
public RuntimeAnimatorController topHeadController;
public RuntimeAnimatorController bottomHeadController;
public float hitPoints = 10;
public Animator TopHead;
public Animator BottomHead;
private bool throwHigh;
public bool canAttack;
void Start()
{
// Start the coroutine for periodic attacks
canAttack = true;
StartCoroutine(AttackPeriodically());
}
IEnumerator AttackPeriodically()
{
while (true)
{
if (canAttack)
{
// Wait for seconds before each attack
yield return new WaitForSeconds(attackTimer);
// Perform the attack
Attack();
}
}
}
void Attack()
{
// Determine if the head should throw high or low
throwHigh = Random.Range(0f, 1f) > 0.5f;
if (throwHigh)
{
Debug.Log("1");
TopHead.Play("TopCharging");
}
if (!throwHigh)
{
Debug.Log("2");
BottomHead.Play("BottomCharge");
}
StartCoroutine(WaitForAnimation());
GameObject projectile = Instantiate(cerberusHead, throwHigh ? highSpawnPoint.position : lowSpawnPoint.position,
Quaternion.identity);
CerberusProjectile projObject = projectile.GetComponent<CerberusProjectile>();
Animator animator = projObject.GetComponent<Animator>();
if (projectile.transform.position == highSpawnPoint.transform.position)
{
projObject.topOrDown = true;
topHeadRenderer.enabled = false;
projObject.parentRenderer = topHeadRenderer;
animator.runtimeAnimatorController = topHeadController;
}
else
{
projObject.topOrDown = false;
bottomHeadRender.enabled = false;
projObject.parentRenderer = bottomHeadRender;
animator.runtimeAnimatorController = bottomHeadController;
}
projObject.backWall = backWall;
projObject.head = this.GetComponent<CerberusHead>();
Rigidbody2D projectileRb = projectile.GetComponent<Rigidbody2D>();
projectileRb.velocity = Vector2.left * throwForce * projectileSpeed; // Adjust speed
canAttack = false;
attackTimer = attackTimerWithAnimation;
}
IEnumerator WaitForAnimation()
{
yield return new WaitForSeconds(6.5f);
}
}
Any idea?
Author
Account Strength
50%
Account Age
2 years
Verified Email
Yes
Verified Flair
No
Total Karma
194
Link Karma
143
Comment Karma
41
Profile updated: 1 day ago
Posts updated: 3 days ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 9 months ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/Unity3D/com...