I need help with my scripts!
Hello, I am new to scripting and wanted to make a simple script for my game. I have the idea written down but since I don't really know how it works I really need someone to fix it for me :) Sorry if this is really simple, but again I am new to this and instead of completely copying from others I want to learn how to fix it so I can make other working ones :) Here's the script, it's in javascript:
var Flash : GameObject;
private static var AimedIN;
function Update () {
if(Input.GetButtonDown("AIM")) {
GetComponent.<Animation>().Play("AIM-IN");
AimedIN = true;
}
}
if AimedIN = true {
if(Input.GetButtonDown("AIM")) {
GetComponent.<Animation>().Play("AIM-OUT");
AimedIN = false
}
}
I think it's easy to understand what I'm trying to do, but I just want a working version :D Thanks in advance for this!
Answer by Ledifen · Aug 14, 2017 at 10:57 PM
Hello
I'm not experienced with JS at all, but here's a few things that might help you if I'm not mistaken. C# and JS have some similar ways to go about it so this might help (before you change things in you script, read everything. I've written my process so the beginning might not be the solution I think is best):
I don't think you can put an If statement outside of a function. Maybe try to put it in the update function with the rest.
I also think there's a problem with your "GetComponent blahblah". For all that I know (and it's not much, so it's maybe wrong), You should get your component in an Awake function and have a var that link to them... Errr. Sorry for the vocabulary ^^. I mean, you should write "private var aimInAnim : Animation;" at the top of your code and do the same for the second animation (of course changing the name "aimInAnim"). That way you declare them properly.
Then in the function Awake (), you do something like "aimInAnim = GetComponent(Animation);" The problem here, is that you're going to have the exact same two "GetComponent" for two different var, which is bad. So th solution I can provide is maybe a little complicated.
You have to deal with The animator instead of the animation. Here a link for my own question that I answered myself that explains how I did it.
You will basically use two animations linked to Any State and create 2 parameters (booleans), one for each animation. Each animation has one condition ===> their assigned boolean is true. And then in you code, use the instructions I provided.
Of course if you follow that solution, you have to declare an animator and not the animations and then enabled it in a start function. You also have to set the boolean parameter to false, and only set it to true when you need the animation. Something like that :
public var animatorName : Animator; private static var AimedIN : boolean = false; // I think that you want it to be false when the game starts...
function Start () { animatorName.enabled = true; animatorName.SetBool("boolean1Name", false); animatorName.SetBool("boolean2Name", false); } function Update () { if(Input.GetButtonDown("AIM")) { animatorName.SetBool("boolean1Name", true); AimedIN = true; } if AimedIN = true { if(Input.GetButtonDown("AIM")) { animatorName.SetBool("boolean2Name", true); AimedIN = false } } }
I'm telling you... I really do not know if what I'm saying is right, especially since I work with C#... I tried anyway.
If you have any question about the animator, ask away, I might (or might not ^^') be able to answer.
Tell me if this helps you in any way.
Hello again, I have followed your tips and looked in other places, and I think my script could work although it still has 1 error I have no clue how to resolve:
private var AimedIN = false;
var AimIN = Animation;
var AimOUT = Animation;
function Update () {
if(Input.Get$$anonymous$$eyDown("mouse 1")) {
if(AimedIN == false); {
GetComponent.<Animation>().Play("AI$$anonymous$$-IN");
this.GetComponent("GunFire").enabled=false;
yield WaitForSeconds(0.6);
this.GetComponent("GunFire").enabled=true;
AimedIN = true;
Debug.Log("Aimed In");
}
}
}
function Update2 () {
if(Input.Get$$anonymous$$eyDown("mouse 1")) {
if(AimedIN == true) {
AimedIN = false;
GetComponent.<Animation>().Play("AI$$anonymous$$-OUT");
this.GetComponent("GunFire").enabled=false;
yield WaitForSeconds(0.6);
this.GetComponent("GunFire").enabled=true;
Debug.Log("Aimed Out");
}
}
}
The error is: Script error (GlockAI$$anonymous$$): Update() can not be a coroutine. Thank you so much for helping! And again, the solution might me really simple but I have no clue for these things :)
oh, didn't see you had answered, sorry.
So, I need info to try and help you.
Why do you use function Update2 ? Is there a specific reason why you don't put your two if statements in the function Update?
The problem with the Coroutine stuff, I think, is that you cannot use "Yield" in update. I think it's for performance reasons, but not sure. What you should do is create another function than you name as you want and put the yield in it. Then you call this function when you need it. :
function Update () { if(Input.Get$$anonymous$$eyDown("mouse 1")) { if(AimedIN == false); { GetComponent.().Play("AI$$anonymous$$-IN"); this.GetComponent("GunFire").enabled=false; wait (); // you call the function called "wait" this.GetComponent("GunFire").enabled=true; AimedIN = true; Debug.Log("Aimed In"); } } } function Update2 () { if(Input.Get$$anonymous$$eyDown("mouse 1")) { if(AimedIN == true) { AimedIN = false; GetComponent.().Play("AI$$anonymous$$-OUT"); this.GetComponent("GunFire").enabled=false; wait (); this.GetComponent("GunFire").enabled=true; Debug.Log("Aimed Out"); } } } function wait (){ //you create the function that uses the yield yield WaitForSeconds(0.6); }
There's also a thing in C# : when you use decimals numbers, you have to add an F after it. That how I understand it. $$anonymous$$aybe hat'll be an issue and you should write "yield WaitForSeconds (0.6F);
Be also careful to this :
if(AimedIN == true) { AimedIN = false;
. Here you set your boolean to false right after checking if it's true, so the condition AimedIN == true is not met anymore and it might give weird results. So maybe put it at the very end of your if statement, after the wait() function.
That's all I can say right now. I'll check more often if I have an answer. And don't worry about "having no clue", as you put it. You'll get there, it juste takes time. :)
Your answer
Follow this Question
Related Questions
How to increase the time between updates? 1 Answer
Beginner Help: Making a Script Public for GetComponent Call on another script 1 Answer
Basic Enemy follow script for Unity five, desperately needed! 0 Answers
Accessing script from another object 1 Answer
How to slowly rotate an object only once using scripting? 1 Answer