- Home /
Trying to translate java-script into c#
Basically dont know how to change this so it fits c# tried alot of combinations to get it to work but no success anyone help ? plssssss using UnityEngine; using System.Collections;
public class CrossAnimate : MonoBehaviour {
GameObject UpCurs;
GameObject DownCurs;
GameObject LeftCurs;
GameObject RightCurs;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
UpCurs.GetComponent < "Animator" > ().enabled = true;
DownCurs.GetComponent < "Animator" > ().enabled = true;
LeftCurs.GetComponent < "Animator" > ().enabled = true;
RightCurs.GetComponent < "Animator" > ().enabled = true;
WaitingAnim();
}
}
void WaitingAnim()
{
yield return new WaitForSeconds(0.1f);
UpCurs.GetComponent < "Animator" > ().enabled = false;
DownCurs.GetComponent < "Animator" > ().enabled = false;
LeftCurs.GetComponent < "Animator" > ().enabled = false;
RightCurs.GetComponent < "Animator" > ().enabled = false;
}
}
Answer by Dragate · Oct 20, 2017 at 01:24 PM
UpCurs.GetComponent<Animator>().enabled = true;
Remove "". Change the rest accordinly.
Thank u very much fixed that bit of code stupid error on my part haha still not sure what i should replace void WaitingAnim() with only thing left giving me an error :D
Oh, didn't notice that. You use WaitForSeconds() in WaitingAnim(). So it needs to be a IEnumerator.
IEnumerator WaitingAnim(){
...
}
You cannot call WaitingAnim the normal way since now it's a IEnumerator. Change
WaitingAnim();
to
StartCoroutine(WaitingAnim());
Answer by Kishotta · Oct 20, 2017 at 07:24 PM
This isn't in response to your question, per se, but a recommendation none the less.
You are calling GetComponent<T>()
a lot. This is horridly slow and inefficient. You'd be much better off with something like this:
using UnityEngine;
using System.Collections;
public class CrossAnimate : MonoBehaviour {
GameObject UpCurs;
GameObject DownCurs;
GameObject LeftCurs;
GameObject RightCurs;
private Animator upAnim;
private Animator downAnim;
private Animator leftAnim;
private Animator rightAnim;
void Awake()
{
// Cache all of these Animators so you don't have to fetch them every time you make a change
upAnim = UpCurs.GetComponent<Animator>();
downAnim = DownCurs.GetComponent<Animator>();
leftAnim = LeftCurs.GetComponent<Animator>();
rightAnim = RightCurs.GetComponent<Animator>();
}
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
upAnim.enabled = true;
downAnim.enabled = true;
leftAnim.enabled = true;
rightAnim.enabled = true;
WaitingAnim();
}
}
void WaitingAnim()
{
yield return new WaitForSeconds(0.1f);
upAnim.enabled = false;
downAnim.enabled = false;
leftAnim.enabled = false;
rightAnim.enabled = false;
}
}
Hi $$anonymous$$ishotta thank u for suggesting this script very useful and much more efficient, i am trying to animate a cross-hair so every time the character in game fires the cross-hair recoils, i have made the animations in preparation for this but cant apply them to this code after i apply the script to a empty object, the script is working but has no animation attached and thus does not display any change in game. Is there any script you could suggest that would allow me to attach my animations and thus animate the cross-hair to do this ? thx
Sure! You could try using a lazy property:
/* I'm only going to write out one of them for brevity */
// Assu$$anonymous$$g this is assigned programatically after initialization
public GameObject UpCurs;
// This will only be set the first time UpAnim is referenced
private Animator _upAnim;
// Lazy Property
Animator UpAnim {
get {
if (!_upAnim) {
_upAnim = Curs.GetComponent<Animator> ();
}
return anim;
}
}
void SomeFunction () {
// _upAnim is null
UpAnim.enable;
// _upAnim has been set and enabled
UpAnim.disable;
// _upAnim is not re-fetched, but it is disabled
}
I hope that makes sense and is helpful! Let me know if not.
Actually he doesn't call GetComponent a lot and it's not really worth changing it. Yes it's generally a better practise when you use a reference to a component more than once to store it for later use wheh possible. However he calls GetComponent four times when he presses down a button and four times again after 100ms. GetComponent is not "horridly slow and inefficient". Yes it has some overhead like pretty much everything you do but nothing in the range that would justify such words.
Oh and when you want to suggest best practises you should simply remove all GetComponent calls all together and just make the Animator fields public / serialized and assign the components directly.
In addition to avoid the coroutine creation overhead you may want to just implement a coroutine like this:
void Awake()
{
StartCoroutine(WaitingAnim());
}
void SetAnim(bool aState)
{
upAnim.enabled = aState;
downAnim.enabled = aState;
leftAnim.enabled = aState;
rightAnim.enabled = aState;
}
IEnumerator WaitingAnim()
{
while (true)
{
while (!Input.GetButtonDown("Fire1"))
yield return null;
SetAnim( true );
yield return new WaitForSeconds(0.1f);
SetAnim( false );
}
}
This would start a single coroutine which runs forever. It waits until the Fire1 button has been pressed in which case the inner while loop ter$$anonymous$$ates and the animation gets triggered. After 100ms the animations get disabled again and the coroutine will again wait in the inner while loop until the key is pressed again.
Of course in this case you can't press the key faster than 10 times a second. However it wouldn't make much sense to allow this as the enabling / disabling of several coroutines would interleave each other and the result would be a mess.