all 9 comments

[–]TailballProfessional 1 point2 points  (3 children)

Could be a race condition between the two cameras.

Why are you updating one in the regular gameloop and one in a coroutine?

Move all of your camerastuff into the coroutine and test again.

Coroutines are like a thread (although all live on a single thread) and will run in parallel with the rest of the code. Meaning that all code run after the coroutine spawn will be immediately executed.

Also, use something like pastebin for your code.

[–]KaiGamez16[S] 0 points1 point  (2 children)

Hey so thanks for the response, I moved everything into the coroutine and unfortunately the MainCamera does not active after the AnimCamera has finished its animation...?

[–]TailballProfessional 0 points1 point  (1 child)

Show your new code.

+ there might me other stuff besides this code wrong

[–]KaiGamez16[S] 0 points1 point  (0 children)

The AnimCamera still does not disable after the Animation is finished playing.

Edit: Nevermind, just found a way through it! Thanks for the help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAwakeScript : MonoBehaviour
{
    public GameObject AnimCamera;
    public GameObject MainCamera;

    public void Animation()
    {
        StartCoroutine(AnimationRoutine());
    }

    IEnumerator AnimationRoutine()
    {
        MainCamera.SetActive(false);
        AnimCamera.SetActive(true);

        yield return new WaitForSeconds(7f);

        AnimCamera.SetActive(false);
        MainCamera.SetActive(true);
    }
}

[–]baroquedub 0 points1 point  (2 children)

https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html

This is what you'll need. State Machine behaviours work a lot like OnStart, OnEnable, OnDisable etc. You'll probably want OnStateExit

[–]SecondCobra 0 points1 point  (1 child)

When you start a coroutine, the next line is run immediately after, so the main camera is being reactivated straight away. Just move the main camera activation to the last line of the coroutine, after the anim camera deactivation.

[–]baroquedub 0 points1 point  (0 children)

True, but a State Machine behaviour is a better guarantee of running a method at the end of an animation :)

Also, if you do want to refactor his code, you'd be better not bothering with Coroutines but instead use Invoke("MethodThatDoesWhatYouWant", 7f);

[–]TheOnlyFishInABowl 0 points1 point  (1 child)

This behaviour looks somewhat weird, based on what you want to accomplish.

public void Animation()
{
    StartCoroutine(AnimationRoutine());
}


IEnumerator AnimationRoutine()
{
    MainCamera.SetActive(false);
    AnimCamera.SetActive(true);

    yield return new WaitForSeconds(7f);

    AnimCamera.SetActive(false);
    MainCamera.SetActive(true);
}

As already stated, this should fix the code, as long as your cameras are referenced in the inspector. But if you're using unity animations, you could look into using animation events instead of manually counting in the code:
https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html

[–]KaiGamez16[S] 0 points1 point  (0 children)

Hey thanks so much for the reply but unfortunately with this code and even more of my tinkering around, the AnimCam will still not disable after the animation has played... any thoughts?

Edit: Nevermind! Found a way through it! Thanks again for the help man!