Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
naam00's avatar
naam00
Honored Guest
11 years ago

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

5 Replies

Replies have been turned off for this discussion
  • drash's avatar
    drash
    Heroic Explorer
    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
    }
  • naam00's avatar
    naam00
    Honored Guest
    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
  • 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 ;)
  • drash's avatar
    drash
    Heroic Explorer
    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).
  • naam00's avatar
    naam00
    Honored Guest
    "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