So I am trying to access an Animation in my script, wait for a moment, then have the other animation play. It seems to work fine, but I am it is saying I have unexpected symbols- “Unexpected symbol )” And then it has three other errors with that message. This is my code below- any help would be appreciated! I am also coding in C#.
void OpenTheDoor (){
TheDoor.GetComponent<“Animator”>().enabled=true;
yield return new WaitForSeconds(1);
TheDoor.GetComponent<“Animator”>().enabled=false;
yield return new WaitForSeconds(5);
TheDoor.GetComponent<“Animator”>().enabled=true;
yield return new WaitForSeconds(1);
TheDoor.GetComponent<“Animator”>().enabled=false;
}
}
You do have an extra closing bracket at the end. So that could be the issue, unless that was copied as the closing bracket for the class, which is why I asked for the rest of the script.
That was what was closing the class, but I am appreciative of the code tags you linked me to. I didn’t know those were a thing, so thanks for pointing it out!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class OpenDoor001 : MonoBehaviour {
public GameObject TextDisplay;
public float TheDistance = PlayerRaycast.DistanceFromTarget;
public GameObject TheDoor;
void Update (){
TheDistance = PlayerRaycast.DistanceFromTarget;
if (Input.GetButtonDown("Action")) {
if (TheDistance <= 2) {
OpenTheDoor();
}
}
}
void OnMouseOver (){
if (TheDistance <= 2) {
TextDisplay.GetComponent<Text>().text = "Press Button";
}
}
void OnMouseExit (){
TextDisplay.GetComponent<Text>().text = "";
}
IEnumerator OpenTheDoor ()
{
Animator anim = TheDoor.GetComponent<Animator>();
WaitForSeconds shortWait = new WaitForSeconds(1f);
anim.enabled = true;
yield return shortWait;
anim.enabled = false;
yield return new WaitForSeconds(5);
anim.enabled = true;
yield return shortWait;
anim.enabled = false;
}
}
So basically this still isn’t working. I would go over to the button, hit e, and nothing will happen. I have the “action” button set to positive button e, but It won’t work. I’m really stumped on this- again, any help is appreciated beyond words!
I used your code and it doesn’t bring any errors, so thank you for that! But if it’s not too much trouble, could you take a look at the script I just posted? The key I am using to press to open the door is not working, and I am truly stumped by it.
You need to figure out where the problem is. Put debug.log lines before and after line 17 to determine if the button press is both being registered and if the value of TheDistance is not what you were expecting. You could also temporarily swap out your Input.GetButtonDown line with Input.GetKeyDown(“e”) to see if the issue is with how you’ve configured the input manager.
When you run into these kinds of issues you should just always start with a liberal use of debug.log
TheDistance = PlayerRaycast.DistanceFromTarget; Make sure this is recalculating distance.
But as @Joe-Censored mentioned. Add a debug to make sure parts of the code are running. Debug values to make sure you are getting the values you expect to.
Debug.Log(TheDistance); for example will help you see if you are getting the expected value.
Thank you for the tip- so I decided to use a debug that says “Hey” When you press the right key to activate the door. The log came up just fine, and continued to do so. The animation just won’t play. I’m not sure what else to do here. The Animation just isn’t being triggered by the button.
I used the Debug.Log, and it works fine- It comes up whenever I hit the correct button while looking at the switch. But the Animation just won’t play when I hit the right button.
Should the door be opening by animation?
If so why are you just enabling and disabling the animator? What do you think that should do?
You should be settings up a parameter in the animator and sending it a trigger.
This shows a clear lack of research if so: https://unity3d.com/learn/tutorials/topics/animation/animator-scripting?playlist=17099
I did put the debug in, yeah. It worked fine. And how do you mean? I’m really new to unity and scripting, and this was a transfer from a javascript. I thought it would have worked where I wrote "
Animator anim = TheDoor.GetComponent();"
The door should be opening by animation, yeah. I would be calling it when I hit the button. And enabling and disabling the animator was actually an accident- I didn’t notice I repeated the line.