Question by
SlimBun · Mar 24, 2020 at 06:12 PM ·
scripting problem3dscript error
StackOverFlowException: The requested operation caused a stack overflow
Hello. I am making a game, and I am getting this error: StackOverFlowException: The requested operation caused a stack overflow
This is my code I think is causing the problem:
private IEnumerator SpawnWallJumpParticle()
{
if (IsWallJumping)
{
Instantiate(WallJumpParticle, transform.position, Quaternion.identity);
yield return new WaitForSeconds(0.3f);
}
yield return StartCoroutine(SpawnWallJumpParticle());
}
This is the full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private CharacterController Controller;
public float Speed = 12f;
public Vector3 Velocity;
public float Gravity = -9.81f;
public float JumpHight = 3f;
public bool IsWallJumping;
private Rigidbody Rigidbody;
public float thrustForward = 1.0f;
public float thrustUp = 1f;
private bool IsGrounded;
public bool IsCutScene;
public ParticleSystem WallJumpParticle;
void Start()
{
StartCoroutine(SpawnWallJumpParticle());
Controller = GetComponent<CharacterController>();
Rigidbody = gameObject.GetComponent<Rigidbody>();
if (IsCutScene)
{
gameObject.GetComponent<Animator>().enabled = true;
}
}
// Update is called once per frame
void Update()
{
if (IsCutScene == false)
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 Move = transform.right * x + transform.forward * z;
Controller.Move(Move * Speed * Time.deltaTime);
StartCoroutine(WallJump());
if (Input.GetKey(KeyCode.Space))
{
Jump();
}
if (Input.GetKey(KeyCode.LeftShift))
{
transform.localScale = new Vector3(1, 0.7f, 1);
Controller.height = 3.8f / 2;
}
else
{
transform.localScale = new Vector3(1, 1, 1);
Controller.height = 3.8f;
}
}
Velocity.y += Gravity * Time.deltaTime;
Controller.Move(Velocity * Time.deltaTime);
if ((Controller.collisionFlags & CollisionFlags.Below) != 0)
{
IsGrounded = true;
Velocity.y = -2f;
}
else
{
IsGrounded = false;
}
}
private void Jump()
{
if (IsGrounded)
{
Velocity.y = Mathf.Sqrt(JumpHight * -2f * Gravity);
}
}
private IEnumerator WallJump()
{
if ((Controller.collisionFlags & CollisionFlags.Sides) != 0)
{
IsWallJumping = true;
Gravity = 0;
Rigidbody.AddForce(transform.forward * thrustForward);
Rigidbody.AddForce(transform.up * thrustUp);
Velocity = new Vector3(0,5,0);
yield return new WaitForSeconds(1f);
Velocity = new Vector3(0,-2f, 0);
}
else
{
IsWallJumping = false;
Gravity = -9.81f;
}
}
private IEnumerator SpawnWallJumpParticle()
{
if (IsWallJumping)
{
Instantiate(WallJumpParticle, transform.position, Quaternion.identity);
yield return new WaitForSeconds(0.3f);
}
yield return StartCoroutine(SpawnWallJumpParticle());
}
}
Comment
Answer by UnityToMakeMoney · Mar 24, 2020 at 06:50 PM
I believe that this part is causing an infinite loop because you always call it:
yield return StartCoroutine(SpawnWallJumpParticle());
I may be wrong, but that is what it seems like to me. I suggest you find a way to not call the entire function in the Start method to check if that is the problem.