diff --git a/core/variant.cpp b/core/variant.cpp index 1a2b4d934b7d0..3a7b08fd0ea79 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -2338,12 +2338,20 @@ Variant::Variant(const RID &p_rid) { Variant::Variant(const Object *p_object) { type = OBJECT; + Object *obj = const_cast(p_object); memnew_placement(_data._mem, ObjData); + Reference *ref = Object::cast_to(obj); + if (unlikely(ref)) { + *reinterpret_cast *>(_get_obj().ref.get_data()) = Ref(ref); #ifdef DEBUG_ENABLED - _get_obj().rc = p_object ? const_cast(p_object)->_use_rc() : NULL; -#else - _get_obj().obj = const_cast(p_object); + _get_obj().rc = NULL; + } else { + _get_obj().rc = likely(obj) ? obj->_use_rc() : NULL; +#endif + } +#if !defined(DEBUG_ENABLED) + _get_obj().obj = obj; #endif }