Ok… That’s really driving me mad! Trying to use C# instead of javascript has definitely been a crazy choice, but, in the end, 2 languages are better than 1, right?
So… I have 2 Questions regarding C# scripting and 1 general question:
- C# is giving me errors in a situation where I declared a variable, precisely
private Animation animator
and then, in the start function I try to assign it with a GetComponentInChildren like this: animator = GetComponentInChildren(Animation);
And this just gives me the usual C# 0119 error: Expression denotes a type', where a
variable’, value' or
method group’ was expected.
Any help?
2)In javascript if i declare a variable, assign it to a script and try to access a variable inside that script, I used to write something like:
function Update()
{
var playerInput : PlayerInputScript = GetComponent(PlayerInputScript);
var currentDirection = playerInput.GetDirection();
var lastKey = playerInput.lastKey;
}
And It works perfectly.
In C# I tryed something like :
private Object playerInput;
playerInput = GetComponent("PlayerInputScript"); //Doing this by string is times slower!
void Update()
{
Vector3 currentDirection= playerInput.GetDirection();
string lastKey = playerInput.lastKey;
}
But this gives me an error, CS0117: UnityEngine.Object' does not contain a definition for
lastKey’
How should I do this?
Last question is:
3) How much interoperability is there between Java and C# scripts? Cons for doing it? Performance problems?
Thanks a lot for any help
P.S. Can someone help me HERE? I am having problems to let Visual Studio work with Unity.
Philip
I think this is the info you need:
A class identifier is not expressed the same way as a type in C# - to get the type of a class you use typeof(class).
if:
string s = “”;
then:
typeof(string) == s.GetType()
I’m not really getting this… Can you be a little bit more clear? maybe make an example…?
Thanks
If I check the type of one of my classes it returns Object… but object is what i am using, can you provide me with some example code?
The confusion is understandable - it’s about the distinction between a type and the type (or class) called Type.
A Type instance contains information about a type, its name, what assembly it belongs to, what its methods and properties/fields are etc.
You can never supply the type itself as an argument in C#, which is what you’re trying to do:
GetComponentInChildren(Animation);
Instead, you need to use the Type that describes the type:
GetComponentInChildren(typeof(Animation));
You also need to downcast the result from Component to Animation, by putting (Animation) in front of it in order to assign it to a Animation variable.
This would be more elegant (and slightly faster) with the use of Generics, but as Unity needs to work on .NET 1.1 on iPhone, this is the way it is.
Oh, Thanks, that’s really something I needed [Thinking about buying a C# book] …
Can you Help me around Point 2 as well please?
Thanks a lot
Philip
I think your first priority should be to get away from that god-awful script editor in Unity, and start using VS or SharpDevelop, and you will learn a lot from autocompletion, realtime error-checking etc.
I’ve never looked in a C# book myself - Visual Studio, with the help of MSDN and Google, have been plenty for learning the language. But then tech books have always bored me, I prefer to try than to be told.
Point 2: Object, that you typed playerInput as, is what everything in .NET derives from, and it doesn’t know jack about “lastKey”. In UnityScript, you have typed playerInput as PlayerInputScript:
var playerInput : PlayerInputScript = GetComponent(PlayerInputScript);
You just need to do the same and type it as PlayerInputScript in C# too (and remember the stuff from point 1!).
I know, but currently vs is not working for me (check the post scriptum in original thread).
hmmm you are driving me crazy… Could you please give me some examples? Maybe let my snippets 1 and 2 work? This makes understanding easier as i am a bit lost…
thanks
philip
I’m curious: What benefits are you looking for by moving to C#?
Well, I am doing it mainly because I want to learn another language; as I am a student, I am trying to “get as much opportunities as i can” so that, when i’ll have to choose, i’ll have various options.
C# seemed interesting, and so i decided to learn it. Plus all the threads in the forum with AngryAnt talking about how better sintax has C#, it made me try to learn that.
Also, I wanted to use Visual Studio… but it’s not working for me anyway
Philip
P.S. Waiting for those examples…
PlayerInputScript playerInput = (PlayerInputScript)GetComponent(“PlayerInputScript”);
or
PlayerInputScript playerInput = (PlayerInputScript)GetComponent(typeof(PlayerInputScript));
I strongly recommend you against continuing down the C# path until you have a decent editor though.
If you have problems getting VS to run, SharpDevelop or even monodevelop are infinitely better than Unitron.