Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

CutScene with some turn off and turn on components.

Discussion in 'Scripting' started by Harardin, Aug 15, 2015.

  1. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Hello everybody I create two scripts and they should start cutscene and end it. Also with some turn off component so player cant move while cutscene, but I am not so experienced with Scripting, and I made a few mistakes because this script doesn’t work at all.

    Here is the first Script what do Raycasting

    Code (CSharp):
    1.  RaycastHit cellPhone;
    2.         Ray phoneRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    3.         if (Physics.Raycast(phoneRay, out cellPhone, PhoneRayDistance))
    4.         {
    5.             if (hit.transform.gameObject.tag == "Phone")
    6.             {
    7.                 if (Input.GetButtonDown("actButton"))
    8.                 {
    9.                     GetComponent<phoneAnimationScript>().CutSceneStart();
    10.                     Debug.Log("RayHitPhone");
    11.                 }
    12.                 Debug.Log("RayHitPhone");
    13.             }
    14.         }
    15.         }
    And a second one what should turn off components and start an animation

    Code (CSharp):
    1. public class phoneAnimationScript : MonoBehaviour {
    2.     private Animator cellPhoneAnimatorCtrl;
    3.     public Animation cellPhoneAnimation;
    4.     private AnimatorStateInfo BaseLayer;
    5.     static int answerPhoneAct = Animator.StringToHash("Base Layer.phoneAnswerAnim");
    6.     private AnimatorStateInfo currentBaseState;
    7.     public CharacterMotor MovementScript;
    8.     public MouseLook CameraMovementScript;
    9.  
    10.     void Start () {
    11.         cellPhoneAnimatorCtrl = GetComponent<Animator>();
    12.         cellPhoneAnimation = GetComponent<Animation>();
    13.         MovementScript = GetComponent<CharacterMotor>();
    14.         CameraMovementScript = GetComponent<MouseLook>();
    15.     }
    16.     public IEnumerator CutSceneStart()
    17.     {
    18.      
    19.             PhoneAnimationStart();
    20.             CharacterAnimationStart();
    21.             yield return new WaitForSeconds(5);
    22.             TurnOnEverything();
    23.      
    24.      
    25.     }
    26.     public void PhoneAnimationStart()
    27.     {
    28.         cellPhoneAnimation.Play("take a phone");
    29.     }
    30.     public void CharacterAnimationStart()
    31.     {
    32.      
    33.         MovementScript.enabled = false;
    34.         CameraMovementScript.enabled = false;
    35.         cellPhoneAnimatorCtrl.SetBool("phoneAnswer", true);
    36.          
    37.     }
    38.     public void TurnOnEverything()
    39.     {
    40.         MovementScript.enabled = true;
    41.         CameraMovementScript.enabled = true;
    42.     }
    43. }
    Can someone help please.
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    This is probably your problem
    Code (csharp):
    1. GetComponent<phoneAnimationScript>().CutSceneStart();
    This doesn't start the function as a coroutine. What you want to do is
    Code (csharp):
    1. phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
    2. StartCoroutine(pas.CutSceneStart());
     
  3. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Just tried your code, animation didn’t starts.

    Alsow DebugWindow say this part of code has a Null reference

    Code (CSharp):
    1. StartCoroutine(pas.CutSceneStart());
     
  4. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    For GetComponent to work, the phoneAnimationScript needs to be on the same gameobject. If it's not, then it will return null.

    If you don't want them on the same gameobject, you can create a reference to it that you assign in the inspector.
    Code (csharp):
    1. public phoneAnimationScript myPhoneAnimationScript;
    2. ...
    3. StartCoroutine(myPhoneAnimationScript.CutSceneStart());
     
  5. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Yeah it give some progress now Animation from phone starts, but character animation is not.

    I tried to add created a reference for character too, but it gives me an error

    Code (CSharp):
    1.     public phoneAnimationScript myphoneAnimationScript;
    2.     public phoneAnimationScript myCharacterAnimationScript;
    3.  
    4.  
    5. if (Input.GetButtonDown("actButton"))
    6.                 {
    7.                     phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
    8.                     StartCoroutine(myphoneAnimationScript.CutSceneStart());
    9.                     StartCoroutine(myCharacterAnimationScript.CutSceneStart());
    10.                 }
    (MissingComponentException: There is no 'Animation' attached to the "Player" game object, but a script is trying to access it.

    You probably need to add a Animation to the game object "Player". Or your script needs to check if the component is attached before using it.)

    My character is controlled by a AnimationController, Should I add separate Animation component for this action?
     
  6. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Animation is the legacy animation system, Animator is the new mecanim system. If you're using an AnimationController, you should be using Animator and not Animation.
     
  7. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Yeah sorry didnt know Animation and Animator dont work good with each other. Thank you so much for help, no kisses but I am sure you understand =)

    Here is fully workable example in case some one will be looking for the same as I. Thanks to steego

    Code (CSharp):
    1. public class phoneAnimationScript : MonoBehaviour {
    2.     private Animator cellPhoneAnimatorCtrl;
    3.     private Animator cellPhonePickUP;
    4.     static int pickPhone = Animator.StringToHash("Base Layer.take a phone");
    5.     static int answerPhoneAct = Animator.StringToHash("Base Layer.phoneAnswerAnim");
    6.     private AnimatorStateInfo currentBaseState;
    7.     public CharacterMotor MovementScript;
    8.     public MouseLook CameraMovementScript;
    9.    
    10.     void Start () {
    11.         cellPhoneAnimatorCtrl = GetComponent<Animator>();
    12.         cellPhonePickUP = GetComponent<Animator>();
    13.         MovementScript = GetComponent<CharacterMotor>();
    14.         CameraMovementScript = GetComponent<MouseLook>();
    15.     }
    16.     public IEnumerator CutSceneStart()
    17.     {
    18.        
    19.             PhoneAnimationStart();
    20.             CharacterAnimationStart();
    21.             yield return new WaitForSeconds(5);
    22.             TurnOnEverything();
    23.             Debug.Log("Corrutine_Start");
    24.        
    25.        
    26.     }
    27.     public void PhoneAnimationStart()
    28.     {
    29.         cellPhonePickUP.SetBool("pickPhone", true);
    30.     }
    31.     public void CharacterAnimationStart()
    32.     {
    33.        
    34.         MovementScript.enabled = false;
    35.         CameraMovementScript.enabled = false;
    36.         cellPhoneAnimatorCtrl.SetBool ("phoneAnswer", true);
    37.            
    38.     }
    39.     public void TurnOnEverything()
    40.     {
    41.         MovementScript.enabled = true;
    42.         CameraMovementScript.enabled = true;
    43.     }
    44. }
     
    steego likes this.
  8. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    968
    Yes, that's quite alright :)