3

I am working with the Google Cardboard plugin in Unity 3D (version 5.3.2). I have a C# script set up that is supposed to access the variable "target" from the script "GvrHead.cs". This is the part of the script (it is called "CrystalDrop.cs"):

        12. public Transform target2;
        13. private GameObject HeadObj;
        14. private GvrHead myScript;
        15.
        16.
        17. void Start () {
        18.     HeadObj = GameObject.Find ("GvrHeadObj");
        19.     myScript = HeadObj.GetComponent(GvrHead);
        20.     myScript.target = target2;
        21. }

But when I run the project in Unity, I get this error:

Assets/CrystalDrop.cs(19,49): error CS0119: Expression denotes a type, where a variable, value or method group was expected.

Does anybody know what is going on?

Umair M
  • 10,298
  • 6
  • 42
  • 74
Henry V
  • 195
  • 1
  • 7

3 Answers3

3

Use this:

    12. public Transform target2;
    13. private GameObject HeadObj;
    14. private GvrHead myScript;
    15.
    16.
    17. void Start () {
    18.     GvrHead.target = target2;
    21. }
Umair M
  • 10,298
  • 6
  • 42
  • 74
  • The answer you had before you edited it worked, actually. When it said: GvrHead.target = target2;. If you change it back I'll mark as accepted answer. – Henry V Jul 21 '16 at 16:20
  • @HenryV I changed it back now. I was not sure if the `target` variable is static or not. I just checked documentation of GVR and its not static thats why I reverted it back. Have Fun :) – Umair M Jul 21 '16 at 16:21
  • Thanks, I manually changed it to static. Accepted and upvoted! :) – Henry V Jul 21 '16 at 16:23
  • Glad to be helpful :) However I won't recommend you to change code from classes included in SDK – Umair M Jul 21 '16 at 16:24
1

Simply change myScript = HeadObj.GetComponent(GvrHead); to myScript = HeadObj.GetComponent<GvrHead>();. That's how to get component with C#, in Unity.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I did that, and now it is saying: Assets/CrystalDrop.cs(19,17): error CS0428: Cannot convert method group `GetComponent` to non-delegate type `GvrHead`. Consider using parentheses to invoke the method – Henry V Jul 21 '16 at 16:00
  • So it wants me to use parenthesis again, which would just give me that error again. – Henry V Jul 21 '16 at 16:01
  • You need to use both the angle brackets and parentheses at the end (empty) - exactly as stated by @Programmer – neopickaze Jul 21 '16 at 16:02
  • Oh, sorry, didn't see that. But when I change that part, it just says: Assets/CrystalDrop.cs(20,26): error CS0176: Static member `GvrHead.target` cannot be accessed with an instance reference, qualify it with a type name instead – Henry V Jul 21 '16 at 16:04
  • @HenryV check my answer – Umair M Jul 21 '16 at 16:04
  • @UmairM Your answer is the-same as mine. You basically posted the-same thing I have in my answer. – Programmer Jul 21 '16 at 16:05
  • @Programmer YES! sometimes it is easy to get the meaning from code rather than words – Umair M Jul 21 '16 at 16:06
  • @UmairM **" it is easy to get the meaning from code rather than words"** That doesn't make sense. I provided a code that solves the error he's having...`myScript = HeadObj.GetComponent();` Did you see that? There is no reason to re-post the whole code if the problem is just one line of code. Just fix the line of code with the problem. Anyways, happy coding! – Programmer Jul 21 '16 at 16:15
  • I was not talking about the code part of your answer. I have experienced this a lot that beginners do not get it unless you tell them something to compare with what they have. :) – Umair M Jul 21 '16 at 16:19
1

Make private GvrHead myScript; to public GvrHead myScript; then the drag and drop the game object with GvrHead script.that might work

Thisara
  • 644
  • 3
  • 11
  • 22