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. Dismiss Notice

PLEASE HELP ME WITH MY SIMPLE SCRIPT!!!!!!

Discussion in 'Scripting' started by gamegirl1984, Nov 13, 2014.

  1. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    I tried unity answers and I got NO help.

    This is such a simple question.
    what I'm trying to do is this:

    I would like to take this script:

    Code (JavaScript):
    1. function OnTriggerEnter (player : Collider){
    2.  
    3. if(player.tag == "Player")
    4.  
    5. GameObject.Find("MYANIMATEDGAMEOBJECT").animation.Play("MYANIMATION");
    I want to add variables for the game object and the animationclip, so that I can then REUSE this script for other animation events in my game. Right now I'm making a new script for EVERY animation and audio event. its very time consuming.
    I capitalized what I would like to be able to edit with variables.
    so it might look something like this:

    Code (JavaScript):
    1. var myGameObject:GameObject;
    2.  
    3. var myanimation :AnimationClip;
    4.  
    5. functionOnTriggerEnter(player :Collider){
    6.  
    7. if(player.tag =="Player")
    8.  
    9. GameObject.Find("myGameObject").animation.Play("myanimation");
    10. }
    however I'm having an issues where unity is giving me a null reference error and says "myanimation" isn't there. And I know why, because I don't have an animation called "myanimation" but that's because I would be adding my actual animation to the var spot in the inspector. How do I go about making this work. I don't want to make a new script for every event. Do I have to add something like:

    Code (JavaScript):
    1.  myanimation = xxxxx
    2. gameobject = xxxxxx
    3.  
    4. SOMEONE PLEASE HELP ME!!!!
     
  2. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
    Couldn't you just make public string values and assign them through the Inspector for each component that uses the script, and have the string members of the component be used for the functions?

    I am not a UnityScripter so I don't fully understand UnityScript/Javascript syntax but I'm asking if this can be done because its doable in C# so I'd assume its doable in Javascript too [syntax may be incorrect, the code below is just to illustrate a point]

    Code (CSharp):
    1. var gameObjectName : String;
    2. var animationName : String;
    3.  
    4. function OnTriggerEnter(player : Collider)
    5. {
    6.     GameObject.Find(gameObjectName).animation.Play(animationName);
    7. }
     
    Last edited: Nov 13, 2014
  3. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    Thanks for you response. And I actually don't know what string values are. And that may be my issue. I only am using javascript right now unless I can find a script that will work for what I'm doing I guess. I just don't want to get confused by learning both at the same time.
     
  4. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102
    I just added the
    Code (JavaScript):
    1.  
    2. var gameObjectName : string;var animationName : string;
    3.  
    to my script and got the following error. the name string does not denote a valid type and blah blah bla. Hmmmmmmmmmmm.....
     
  5. Stoven

    Stoven

    Joined:
    Jul 28, 2014
    Posts:
    171
  6. gamegirl1984

    gamegirl1984

    Joined:
    Nov 6, 2014
    Posts:
    102