Skip to main content
Up and Coming
July 13, 2014

Attach To Camera help

Hey Guys,

Since the forum doesn't allow me to search for the string AttachGameObjectToCamera (I guess it's too long?), I suspect this may have been answered elsewhere, but other queries lead to nothing, so here goes.

When I try to attach something to the OVR camera with the provided function, Unity complains that I can't pass a GameObject as a ref parameter. I'm not sure why the OVR functions all mainly use ref – I'm no hard-core programmer at all, so I may be missing something obvious - but it seems that in this case we'd need a simple 'normal' parameter?

Any help greatly appreciated!
Naam
    This topic has been closed for replies.

    5 replies

    Inspiring
    July 13, 2014
    In your call to a function/method with a ref parameter, you'll have to explicitly put "ref" before your argument. I'm guessing that's the issue you ran into.

    Like this:
    if(AttachGameObjectToCamera(ref yourGameObject))
    {
    //Attach was successful
    }
    else
    {
    //Attach failed
    }
    Titans of Space PLUS for Quest is now available on DrashVR.com
    naam00Author
    Up and Coming
    July 13, 2014
    Does that work at your end? I tried that, but I get the mentioned error as soon as I do (though I do not use the return value).

    ovrCam.AttachGameObjectToCamera( ref gameObject );

    ... leads to Unity complaining about ...

    Assets/dialogSystem/DialogMessage.cs(62,54): error CS0206: A property or indexer `UnityEngine.Component.gameObject' may not be passed as `ref' or `out' parameter

    Which would mean the OVR functions is wrongly constructed (needing a ref where a ref is illegal). Not a huge problem in the attach case (as all it does is some reparenting currently, and I can do that myself : ), but just wondered if it would be wiser to use the OVR functions.

    Thanks,
    Naam
    /\/ /\ /\ /\/\____________________________________________________
    Up and Coming
    July 13, 2014
    Hi Naam,
    if you do like drash mentions, then it should work!
    I have just tested some code and it works!

    using UnityEngine;
    using System.Collections;

    public class testingOVR : MonoBehaviour
    {
    public OVRCameraController ovrCam;
    public GameObject playerObject;

    void Start ()
    {
    ovrCam.AttachGameObjectToCamera( ref playerObject );
    }

    void Update ()
    {
    if (ovrCam.AttachGameObjectToCamera(ref playerObject))
    {
    Debug.Log("you have: " + playerObject + " as connected object!");
    }
    else
    {
    Debug.Log("you don't have an connected object!");
    }
    }
    }


    I post it here for you to toy with :)
    I hope this helps somehow ;)
    ASUS Maximus VII Formula Intel core i7 quad core 4790K OC'ed @ 4.4Ghz 32GB DDR3 Ram Nvidia Geforce GTX 980 4GB Windows 10 Pro 64bit GSync Monitor @ 2560x1440, 144Hz ------------------------------------------- DK2 Owner CV1 Preordered.
    Inspiring
    July 13, 2014
    A property or indexer `UnityEngine.Component.gameObject' may not be passed as `ref' or `out' parameter

    Aha! "gameObject" is a property of the MonoBehaviour you're in, not a variable. Properties have get/set accessors that can potentially do more than just retrieve and assign the value to a variable, so it makes sense that they can't be passed in by ref that way.

    You can probably just do it like standtall007 suggested, or if you're trying to attach the GameObject you're in:
    GameObject thisGameObject = this.gameObject;
    ovrCam.AttachGameObjectToCamera( ref thisGameObject );

    Note that if you're trying to attach a different gameObject that you've created other than the one you're in, try using a different name for it because "gameObject" is already taken (the property on the MonoBehaviour).
    Titans of Space PLUS for Quest is now available on DrashVR.com
    naam00Author
    Up and Coming
    July 13, 2014
    "drash" wrote:
    Aha! "gameObject" is a property of the MonoBehaviour you're in, not a variable. Properties have get/set accessors that can potentially do more than just retrieve and assign the value to a variable, so it makes sense that they can't be passed in by ref that way.

    Aha! Makes perfect sense. Thanks!

    Anyone know why Oculus chooses to do this all by ref parameters, though?

    Cheers,
    Naam
    /\/ /\ /\ /\/\____________________________________________________