Search Unity

Main Menu Script C# help me plz

Discussion in 'Scripting' started by DrSega, Apr 22, 2012.

  1. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    Hey everybody, i am making a main menu with C# for my game and i have made the "Quit" script to exit the application, now i have another 3d text saying "Select Map". I want it to be so that if you click on "Select Map" it will play an animation, how do i do this? I have tried for 2 hours now and i cant find out how. Here is my script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MapSelect : MonoBehaviour
    6. {
    7.     public bool isMap = true;
    8.    
    9.     void OnMouseEnter()
    10.     {
    11.         renderer.material.color = Color.blue;
    12.     }
    13.    
    14.     void OnMouseExit()
    15.     {
    16.         renderer.material.color = Color.white; 
    17.     }
    18.    
    19.     void OnMouseDown()
    20.     {
    21.         if(isMap)
    22.         {
    23.             animation.Play(FlyAnim);
    24.         }
    25.     }
    26. }
    27.  
    The error i get is "The name 'FlyAnim' does not exist in the current context.
     
    Last edited: Apr 22, 2012
  2. herpderpy

    herpderpy

    Joined:
    Mar 9, 2010
    Posts:
    477
    Okay, this is quite a simple error and you can get it fixed quite easily.

    The reason you're getting the error is that you haven't defined the object 'FlyAnim'. Go back to where you defined 'isMap' and below it put this line:

    Code (csharp):
    1. Public Animation FlyAnim;
    Then go back to the editor window and drag the animation onto the 'FlyAnim' attribute in the editor.

    Try your script now, it should work.
     
  3. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    I will get an error, this is what my script looks like now

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MapSelect : MonoBehaviour
    6. {
    7.     public bool isMap = true;
    8.     public Animation FlyAnim;
    9.    
    10.     void OnMouseEnter()
    11.     {
    12.         renderer.material.color = Color.blue;
    13.     }
    14.    
    15.     void OnMouseExit()
    16.     {
    17.         renderer.material.color = Color.white; 
    18.     }
    19.    
    20.     void OnMouseDown()
    21.     {
    22.         if(isMap)
    23.         {
    24.             animation.Play(FlyAnim);
    25.         }
    26.     }
    27. }
    28.  
    The error i get now is:

    The best overloaded method match for 'UnityEngine.Animation.Play(UnityEngine.PlayMode)'has some invalid arguments
    Argument '1': cannot convert from UnityEngine.Animation' to 'UnityEngine.PlayMode'
     
  4. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    have you put the animation on the same object as the code. In that case you dont even need the public Animation thing.
     
  5. DrSega

    DrSega

    Joined:
    Mar 6, 2012
    Posts:
    157
    I got it working nvm :) that post helped so much :D but i dna how i did it lol, i just edited some randoms stuff and it works ty guys.
     
unityunity