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

No errors, but not working. Anything wrong with this script?

Discussion in 'Scripting' started by cristo, Nov 25, 2014.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi,

    I attempted converting a Java script into a C# and the console and monodevelop read no errors.

    Yet the door isn't opening.

    Before I focus on working out what I've done wrong through the inspector, I was wondering whether there is anything wrong with this door opening code:

    public class Doorscript : Monodevelop {

    Gameobject Door;

    public void OnTriggerEnter (Collider other) {

    if (other.tag == "Player")

    {
    Gameobject.Find("Door").animation.Play ("door open");
    }
    }
    }
     
    Last edited: Nov 25, 2014
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    onTriggerEnter should be OnTriggerEnter with a capital O, for one.
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Ah sorry that was a mistype, it does have a capital O. I might change that....
     
  4. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    I refuse to believe that MonoDevelop doesn't give you any errors, because mine goes red immediately. :) Also, have a look on how to use code tags properly.

    Your code has several errors:

    1. Monodevelop should be MonoBehaviour.
    2. Gameobject should be GameObject.
    3. onTriggerEnter should be OnTriggerEnter.
    4. The Door variable is never assigned.
    Here's an untested revision:

    Code (CSharp):
    1. public class Doorscript : MonoBehaviour
    2. {
    3.     GameObject Door;
    4.  
    5.     void Start ()
    6.     {
    7.         Door = GameObject.Find ( "Door" );
    8.     }
    9.  
    10.     public void OnTriggerEnter ( Collider other )
    11.     {
    12.         if ( other.tag == "Player" )
    13.         {
    14.             Door.animation.Play( "door open" );
    15.         }
    16.     }
    17. }
     
    Last edited: Nov 25, 2014
  5. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    B
    Bliss, it works.
    Thanks so much for your help toreau.