Search Unity

How do I animate a door to open with C#?

Discussion in 'Scripting' started by Rockinator, Jul 26, 2014.

  1. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    I have a variable in a separate script for when a player collects an item, "itemCounter"; how would I go about making a script to check for collision of "Player" to animate a door, "doorOpen", while checking if "itemCounter" is greater than 0?
     
  2. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    using UnityEngine;
    using System.Collections;

    public class doorOpen : MonoBehaviour
    {
    void onTriggerStay(Collider other)
    {
    if(other.gameObject.tag == "Player")
    {
    if(Input.GetButtonDown(KeyCode.E) && itemCounter == 1)
    {
    animation.Play("doorOpen");
    }
    }
    }
    }

    This is what I am working with.. as you can see, I am not good at coding yet..
     
  3. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Help please..
     
  4. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    At first glance your code looks correct. What I often do is putting print("Yay!") starting from the outer areas of a function and moving it more and more into the inner-most conditions to find out where it doesn't reach. If you're checking values, you can also do print(other.gameObject.tag) to see what the actual value is compared to the one you're expecting.

    Edit: Also try OnTriggerStay instead of onTriggerStay... the other day somebody mentioned on Twitter that OnTriggerStay2d doesn't get called but OnTriggerStay2D does.
     
  5. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Assets/doorOpen.cs(10,34): error CS1502: The best overloaded method match for `UnityEngine.Input.GetButtonDown(string)' has some invalid arguments

    Assets/doorOpen.cs(10,34): error CS1503: Argument `#1' cannot convert `UnityEngine.KeyCode' expression to type `string'

    These are the two errors that have come up..
     
  6. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Okay, I changed the word Button to Key and it alleviated the error. Now a new error took it's place:

    Assets/doorOpen.cs(10,59): error CS0103: The name `itemCounter' does not exist in the current context

    I have two other scripts using the itemCounter, am I supposed to call the itemCounter somehow in my door script?
     
  7. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Try GetKeyDown ... GetButtonDown is for input controls you have defined in your project.
     
  8. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    I assume itemCounter is then stored in a component of the object that's in your trigger. You can get to that by calling GetComponent on the game object to retrieve the component instance where it is stored.
     
    Rockinator likes this.
  9. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    are you trying to access a var from another script? cos the script you just put in doesn't have an itemcounter...

    if you are trying to access a var from another script, make the var a static by putting "static" right after "public" and then in this script put "nameofscriptwithitemcounter." infront of itemcounter (obviously replacing the name) (keep the dot at the end as you can guess)
     
    Rockinator likes this.
  10. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    I missed one last thing, "Item" in front of the itemCounter. It is supposed to call the script Item so that I can get my itemCounter out. So, it's "Item.itemCounter"
     
  11. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Exactly pjde_! Thanks!
     
  12. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Now I am gonna test it out and see if it works!

    EDIT: It doesn't seem to respond when the E button is pressed.. Just to recap what I did in my level, I made a door pivot out of an empty game object, made a cube into a door and have made another cube that is not rendering the mesh as the collider.

    The pivot was set with the animation component with the animation called "doorOpen", the animation size is 1 and the element 0 is the "doorOpen". Play automatically and animate physics were left unchecked. Culling type was set to based on Renderers.

    the door was left as it is.

    the cube acting as the trigger was given the script:

    using UnityEngine;
    using System.Collections;

    public class doorOpen : MonoBehaviour
    {
    void onTriggerStay(Collider other)
    {
    if(other.gameObject.tag == "Player")
    {
    if(Input.GetKeyDown(KeyCode.E) && Item.itemCounter == 1)
    {
    animation.Play("doorOpen");
    }
    }
    }
    }

    What am I missing?
     
    Last edited: Jul 26, 2014
    peterdeghaim likes this.
  13. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Anyone? Man.. I'm so close to getting this to work!
     
  14. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    What did you find out using the print trick?
     
  15. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    well the animation should be for the door if im not mistaken. having a hard time reading into what you were saying but animation.play only works on animations attatched to the animator of an object...
     
    Rockinator likes this.
  16. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    The script I wrote went onto the trigger collider box I made, right now I need to find a way to trigger the animation within the door, how do I do this?
     
  17. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    Solved: All i needed was to create a new animation within the scripted object. Done ^^ thanks ya'll!
     
    peterdeghaim likes this.
  18. peterdeghaim

    peterdeghaim

    Joined:
    Apr 10, 2014
    Posts:
    154
    no problem man!
    ^^ do this too