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

Please Help Me, Script Help Errors Why?

Discussion in 'Animation' started by measlystormnetworkuk2020, Sep 24, 2022.

  1. measlystormnetworkuk2020

    measlystormnetworkuk2020

    Joined:
    Sep 2, 2022
    Posts:
    7
    Hi there I am trying to script my animation for my game, but its coming up with errors that I dont understand, as I am still new to this and learning. Please can someone help me please?

    Screenshot_2.png Screenshot_3.png
     
  2. measlystormnetworkuk2020

    measlystormnetworkuk2020

    Joined:
    Sep 2, 2022
    Posts:
    7
    public class FlameAnimations : MonoBehaviour {

    public int LightMode;
    public GameObject FlameLight;


    void Update () {
    if (LightMode == 0) {
    StartCoroutine (AnimateLight ());
    }

    }

    IEnumerator AnimateLight () {
    LightMode = Random.Range (1, 4);
    if (LightMode == 1) {
    FlameLight.GetComponent<Animation> ().Play ("TorchAnim1");
    }
    if (LightMode == 2) {
    FlameLight.GetComponent<Animation> ().Play ("TorchAnim2");
    }
    if (LightMode == 3) {
    FlameLight.GetComponent<Animation> ().Play ("TorchAnim3");
    }
    yield return new WaitForSeconds (0.99f);
    LightMode = 0;

    }
    }
     
  3. Franco_Voisard

    Franco_Voisard

    Joined:
    Apr 30, 2018
    Posts:
    20
    The problem with your code is that you are missing the using directives. Without them the compiller does not know from where to take the types and methods that you use in your code.

    Without using directives, you are not importing the libraries.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4.  
    5. public class FlameAnimations : MonoBehaviour {
    6.  
    7.     public int LightMode;
    8.     public GameObject FlameLight;
    9.  
    10.  
    11.     void Update () {
    12.         if (LightMode == 0) {
    13.             StartCoroutine (AnimateLight ());
    14.         }
    15.  
    16.     }
    17.  
    18.     IEnumerator AnimateLight () {
    19.         LightMode = Random.Range (1, 4);
    20.         if (LightMode == 1) {
    21.             FlameLight.GetComponent<Animation> ().Play ("TorchAnim1");
    22.         }
    23.         if (LightMode == 2) {
    24.             FlameLight.GetComponent<Animation> ().Play ("TorchAnim2");
    25.         }
    26.         if (LightMode == 3) {
    27.             FlameLight.GetComponent<Animation> ().Play ("TorchAnim3");
    28.         }
    29.         yield return new WaitForSeconds (0.99f);
    30.         LightMode = 0;
    31.  
    32.     }
    33. }