sneakyevil / IL2CPP_Resolver

A run-time API resolver for IL2CPP Unity.
https://sneakyevil.gitbook.io/il2cpp-resolver/
The Unlicense
323 stars 65 forks source link

Crash when try to access GetName() #34

Closed Shillersan closed 7 months ago

Shillersan commented 7 months ago

I tried to use the basic example to loop over GameObjects

std::string m_sObjectSubstring = "Player";

// Obtain list
Unity::il2cppArray<Unity::CGameObject*>* m_pObjects = Unity::Object::FindObjectsOfType<Unity::CGameObject>(UNITY_GAMEOBJECT_CLASS);

for (uintptr_t u = 0U; m_pObjects->m_uMaxLength > u; ++u)
{
    if (m_pObjects->m_pValues[u] == NULL) continue; // I added this because some values were NULL

    Unity::CGameObject* m_pObject = m_pObjects->m_pValues[u];
    if (!m_pObject) continue; // Just in-case

    // Obtaining object name and then converting it to std::string
    std::string m_sObjectName = m_pObject->GetName()->ToString();
    if (m_sObjectName.find(m_sObjectSubstring) != std::string::npos)
    {
        // logger.AddLog("Found Player!");
        break;
    }
}

I get an access violation error here: std::string m_sObjectName = m_pObject->GetName()->ToString();

extremeblackliu commented 7 months ago

its little bit confusing of looping array, but you should use Unity::CGameObject* m_pObject = m_pObjects->m_pValues->operator[](u); or Unity::CGameObject* m_pObject = m_pObjects->m_pValues->At(u);

Shillersan commented 7 months ago

its little bit confusing of looping array, but you should use Unity::CGameObject* m_pObject = m_pObjects->m_pValues->operator[](u); or Unity::CGameObject* m_pObject = m_pObjects->m_pValues->At(u);

Sorry if I am missing something but neither of these are defined. Cannot resolve symbol 'operator[]' Cannot resolve symbol 'At'

extremeblackliu commented 7 months ago

you have probably missing import header files. the function is defined at https://github.com/sneakyevil/IL2CPP_Resolver/blob/main/Unity/Structures/il2cppArray.hpp#L17-L25

Shillersan commented 7 months ago

you have probably missing import header files. the function is defined at https://github.com/sneakyevil/IL2CPP_Resolver/blob/main/Unity/Structures/il2cppArray.hpp#L17-L25

I see, I think you meant this: Unity::CGameObject* m_pObject = m_pObjects->At(u);, which worked! Thank you.

extremeblackliu commented 7 months ago

Author

my fault.