versions 1 2 of PCScript were written in javascript and this code worked fine, but V3 had to be translated into c# to allow class inheritance and these four lines are now throwing up an error that I can well understand (Assets/PCScriptV3.cs(25,49): error CS0119: Expression denotes a type', where a variable’, value' or method group’ was expected) give the structure of C#, and I suspect relatedly similar lines of code in other scripts in the game trying to access these PCScripts are returning unknown identifier errors (which was not happening with the javascript version of the script).
You need to use the typeof keyword, and also cast the return type.
public void OnMouseDown()
{
(GameObject.Find("PC1").GetComponent(typeof(PCScriptV3)) as PCScriptV3).selected = name;
(GameObject.Find("PC2").GetComponent(typeof(PCScriptV3)) as PCScriptV3).selected = name;
(GameObject.Find("PC3").GetComponent(typeof(PCScriptV3)) as PCScriptV3).selected = name;
(GameObject.Find("PC4").GetComponent(typeof(PCScriptV3)) as PCScriptV3).selected = name;
}
Note that if at any point of this code null is returned, eg. the gameobject can’t be found, the component isn’t on there, etc., you will get null reference exceptions. It’s best to grab the gameobjects in a variable, then grab the component from that gameobject and store it in a variable, then apply changes to that component, checking the variables aren’t null along the way.
This may seem like a long way around, but you will end up with safer code in the long run.
Also if those are the only objects in the scene that have that script on them, you can grab them all in an Array by using
It’s already pretty much done now, but for future reference, you wouldn’t, perchance, know where some documentation on that fuctionality is becasue i’ve used javascript a fair bit and never come accross it.
Thanks for the solution Murcho, I’ll give it a try
Basically every .JS file you wrote is already a class. Unity simply imagines the class definition around it:
class MyScript extends MonoBehaviour
{
function Start()
{
// ....
}
// etc...
}
If you want to write your own class, inheriting from another, you can simply explicitly define this class definition yourself and change the inheritance just as you would in C#. (Tbh, my syntax may be off, as it has been a while since I used JS, but I seem to recall the only difference between C# and JS is the ‘extends’ keyword instead of ‘:’).
Offtopic:
Well, the fact that it can work with inheritance wouldn’t automatically imply it can create inheritance :P. For example: Unity JS can use interfaces (like IEnumerator whenever you use yield), but you can’t actually define any yourself (using JS).
Just to clarify: my remark about what is not possible in JS was about interfaces not inheritance. Inheritance can be created in JS just as I described. There is no need to switch to C# if all you need is a custom inheritance struture.
If you do decide to go with C#, for whatever reason, it’s really best to just write all your scripts in C#. Mixing languages is possible, but quite a hassle.
The following page describes how to set compilation order for scripts of different languages:
Note that 2-way communication between scripts of different languages is only possible using the relatively expensive SendMessage function.