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?
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…
Help please…
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.
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…
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?
Try GetKeyDown … GetButtonDown is for input controls you have defined in your project.
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.
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)
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”
Exactly pjde_! Thanks!
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?
Anyone? Man… I’m so close to getting this to work!
What did you find out using the print trick?
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…
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?
Solved: All i needed was to create a new animation within the scripted object. Done ^^ thanks ya’ll!
no problem man!
^^ do this too