WHAT IS WRONG WITH THIS CODE?
So I am trying to setup a line of code that changes a parameter from 0 to 1 when the W key is pressed down, but I keep getting an error with the symbol "public." I don't know what's wrong. This is my C# code:
using UnityEngine; using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.W) public void SetFloat("Forward", 1));
}
}
are you trying to set an animation parameter (using SetFloat()
)? if so, you'll need to get a reference to it first.
public void blah-blah-blah
is how you declare a function, not call one.
your line should look something like this:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
{
// do something
}
the 'do something' part might be (it depends on what you're really trying to do):
_anim.SetFloat("Forward",1);
where _anim
has been set to the animator component.
best advice for you - you might want to spend time with some c# tutorials...
Ok... I followed that but and here's my code:
using UnityEngine; using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
{
_anim.SetFloat("Forward",1)
**}**
}
}
But it's having parsing problems on the symbols surrounded by ** or bold. Also, how do I set up the _anim to the animator component. I will look more into c#.
@gjf : Friendly re$$anonymous$$der of the FAQ :
FAQ :
What are the guidelines for writing good questions?
We want Unity Answers to become a comprehensive database of questions and answers related to Unity. When you ask your questions, it can be helpful to keep a few things in $$anonymous$$d:
Don't post multiple unrelated questions in one post. $$anonymous$$ake separate posts ins$$anonymous$$d, each with one question.
Don't write things that will be irrelevant in a few days. Writing that "This is urgent!" will probably not get you faster answers, but it may make people frown at your question.
Be specific. If you want specific answers, ask specific questions. If something "doesn't work", write what exactly it does that is wrong.
Use specific tags. unity, scripting, editor, etc. are very non-specific tags. Consider using tags that are more specific to your exact question. Tags are case sensitive, so if you can't find the tag you want to use in the drop-down list, try using lower/upper case letters as an alternative. Here is the full list of tags.
Break multi-part problems into simpler questions. Ins$$anonymous$$d of posting a question: "How can I make a gui toggle to change the skybox?", post two simpler questions ins$$anonymous$$d: "How can I make a gui toggle?" and "How can I change the skybox?" This way the questions will also be relevant to people who want to make a toggle for something else, or who want to change the skybox based on something else.
Some reasons for getting a post rejected:
You haven't provided enough context: we need more information about your problem, relevant code snippets and what you have tried already.
There exists duplicate questions with answers if you were to do a search, either on Answers or on the Forum
Your question isn't specific enough: asking for a script, asking multiple questions in one post or asking a question that could be either subjective or require extensive discussion (and thus belongs in the Forum)
Badly formated question: code needs to be formatted using the 101/010 button, break your post up into several parts so it's understandable what you are asking, and make sure there is a question in your post
Asking us to fix your code: more than likely, you simply need to get a better understanding of basic program$$anonymous$$g. Having a look at the Scripting tutorials on the Learn page will help you become more familiar with scripting in Unity
Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue
@Hellium: thanks, i'm well aware of the FAQ ;)
rather than telling me that, in addition to pointing out the missing semicolon, you might have helped the OP in the update regarding how they set up the animator component reference.
in Start()
you need to get a reference to the animator component, so add a line like this:
_anim = GetComponent<Animator>();
and before Start()
declare the following:
private Animator _anim;
When I see a question where the majority of the requirements are not satisfied, I rarely take the time to answer the OP. Especially when there are a thousand of question about the subject, tutorials about the topic on Unity Learn, etc....