I am switching over from javascript and I am confused about some of the variable types. What types of variables are these below (written in javascript) and how do you set them: var myController = freyaPlayer.GetComponent.<CharacterController>(); anim = GetComponent.<Animation>(); Thanks!
Should be: Code (csharp): CharacterController myController = freyaPlayer.GetComponent<CharacterController>(); Animation anim = GetComponent<Animation>(); // could also be: this.GetComponent<Animation>(); You can use var in C# too, but it's less explicit, which is why I would advise against it.
I tried the variable like this and get an error: CharacterController myController = freyaPlayer.GetComponent.<CharacterController>(); error: Unexpected symbol `<', expecting `identifier'
ok, now I try this and get another error: Animation anim = GetComponent<Animation>(); A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent(System.Type)'
You can't make a GetComponent call outside of any function. So if you have ANY variable in your class that uses GetComponent to initialize you have to call GetComponent from within some function. So this would work: Animation anim; void Start() { anim = GetComponent<Animation>(); } And this should not work (unless you put the entire line in a function): Animation anim = GetComponent<Animation>();
Thank you very good explanation. One last quick example of a variable. Let's say I set something up like this, I am using this new variable incorrectly and getting an error, what needs to change? Transform playDeath = GameObject.FindWithTag("FreyaArms").transform; playDeath.GetComponent<FreyaArms>().playDeath = true; error: Assets/Scripts/Freya.cs(240,25): error CS0246: The type or namespace name `FreyaArms' could not be found. Are you missing a using directive or an assembly reference?
That error usually means you either spelled something wrong or the script you're refering to doesn't exist/can't be found. By the way you can't do a GetComponent call on a transform. Look again, you declared the playDeath variable as a Transform type, yet on the next line you try to call GetComponent on it, which you can only do on GameObject types.
So is the type wrong? I tried GameObject before and I think I got an error. I must be declaring the variable wrong because it works in javascript.
I forgot to mention that what I explained about GetComponent calls also counts for GameObject.Find and/or any other function. You can't call functions (because that's what they are) outside of a function.
Well, that should work. I find it a bit unusual that both the component and the tag have the same name ... but maybe that's correct. Quick note: That doesn't look like good coding style. Look at something like: Code (csharp): GameObject.FindWithTag("FreyaArms").SendMessage ( "OnSetPlayDeath" ); where the OnSetPlayDeath function would then set the variable. SendMessage is not that great either, though. Look at the techniques described here. Actually you can, I assume they added that for conveniance. I guess Code (csharp): transform.GetComponent<Component>(); internally gets converted to Code (csharp): transform.gameObject.GetComponent<Component>(); You can also do Code (csharp): transform.SendMessage ( "OnEvent" );
Thanks for the answer you are being very helpful. I hate c# haha. If you have time, I am also getting an error on this IEnumerator. // Left Cast Out public bool playLeftCastOut = true; IEnumerator LeftCastOut() { playLeftCastOut = false; GetComponent<Animation>().CrossFade("FA_Idle"); playLeftCastOut = true; } error: : not all code paths return a value
You must use a yield statement in a coroutine. You're treating that like any typical function. If you want it to be, you would put 'void' instead of IEnumerator. You would use a coroutine if you want to delay execution of the function or wait for another function to finish running. In this case, if you want to wait until the CrossFade call is finished before setting 'playLeftCastOut' to true, you can yield for the duration of the fade by saying: yield return new WaitForSeconds(0.3f); (It seems CrossFade's default duration is 0.3, but you would put whatever length you set).
Thanks man that fixed the errors I was getting, I had like 6 of those. Very helpful knowledge I appreciate the help