Search Unity

Collecting score and starting animation of item.

Discussion in 'Scripting' started by Jairusx, Jul 18, 2020.

  1. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Hello my friends! I am trying to accomplish something really easy but i am experiencing some issues.
    I created simple collecting script, that allows me on triggerEvents to collect spheres (for example) then update my score. This part was easy.But now, i am trying to link this with an object that is underground (for example big arch or something) and when i collect the full amount of items (eg. when i update my score) i need this arch or door to appear from underground with animation. (cuz there is the quest item i needed for example) It may be teleporting or appearing too.Not necessary to be underground. I created the animation, but now i don't know how to link them together.I assume i must went back in collecting script and where i update my score, i need to check if score is == to score that need for trigger the animation.But then i don't know what to do.
    Many thanks in advance!!! <3
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    That's awesome. Without seeing code, hard to say anything specific, but I know there are TONS of tutorials out there that do all of the above things, and once you understand the key steps in each phase, you can put them together however you like. I highly recommend Brackey's Tutorials. He's really good. But the Unity tutorials linked above under Learning are also good.
     
    Jairusx likes this.
  3. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Thank you very much for the fast reply! I have this very basic script for collecting items.Here is the script:


    1. Code (CSharp):
      1. using Packages.Rider.Editor.UnitTesting;
      2. using System.Collections;
      3. using System.Collections.Generic;
      4. using UnityEngine;
      5. using UnityEngine.SocialPlatforms.Impl;
      6. public class SpherePoint : MonoBehaviour
      7. {
      8.  
      9.     private void OnTriggerEnter(Collider fpsController)
      10.     {
      11.         if (fpsController.transform.name == "FPSController")
      12.         {
      13.             KeepScore.Score += 100;
      14.             Debug.Log("Hit the ball");
      15.             Destroy(gameObject);
      16.         }
      17.     }
      18. }


    I have a little script for scoreboard that keeps score -
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class KeepScore : MonoBehaviour
    5. {
    6.     public static int Score = 0;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.      
    11.     }
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.      
    16.     }
    17.     void OnGUI()
    18.     {
    19.         GUI.Box(new Rect(100, 100, 100, 100), Score.ToString ());
    20.     }
    21. }
    Now i've created a basic cube that i put underground and animated it to go up. I made the cube to play the animation only once.Now when i play the game the animation activates and it goes up. Ok, i need now to make the cube stays "idle" and when i collect x items, then to trigger the animation. I tried something like this -
    Code (CSharp):
    1. public class TestAnimationScript : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.  
    5.     string animationName = "Test";
    6.     void Start()
    7.     {
    8.        
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (KeepScore.Score == 400)
    15.         {
    16.             GetComponent<Animation>().Play(animationName);
    17.  
    18.         }
    19.    
    20. }
    21. }
    22.  
    but i get this error
    MissingComponentException: There is no 'Animation' attached to the "AnimationTest" game object, but a script is trying to access it.
    You probably need to add a Animation to the game object "AnimationTest". Or your script needs to check if the component is attached before using it.
    UnityEngine.Animation.Play (System.String animation) (at <049be2afe36f487eb06ef49d51a0bab6>:0)
    TestAnimationScript.Update () (at Assets/TestAnimationScript.cs:20)

    Many thanks!!! <3
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    There is no possible way I can express this any clearer than the error text above does. That is literally what you must do to fix this. If you don't know how to do that, work through some Unity animation / Mecanim / Animator tutorials.
     
    Jairusx likes this.
  5. Jairusx

    Jairusx

    Joined:
    Jun 25, 2020
    Posts:
    62
    Thanks.I was using animation instead of animator.I changed GetComponent<Animation>().Play(animationName) to Animator and it worked :)