Why your current hook works and a button does not
ConsumesMoney hands you a live
instance as its first argument, so you never had to look one up. A menu button runs in your own code with no
this pointer, and
AddMoney is an instance method, so calling
AddMoney(NULL, 1234) crashes or silently no-ops depending on whether the body dereferences
this.
You have three options. Take the first one.
1. Resolve the instance once and cache it
rrosetta is pointing at this, but "do FindObjectByType" is only half of it. Find the class that owns the offset in dump.cs first, then check whether it exposes a static singleton field. If it does, read the field, because that is far cheaper and works even when the object is inactive.
Code:
static void* g_wallet = nullptr;
void* ResolveWallet() {
if (g_wallet) return g_wallet;
Il2CppDomain* dom = il2cpp_domain_get();
size_t n = 0;
const Il2CppAssembly** asms = il2cpp_domain_get_assemblies(dom, &n);
for (size_t i = 0; i < n; i++) {
Il2CppImage* img = il2cpp_assembly_get_image(asms[i]);
Il2CppClass* cls = il2cpp_class_from_name(img, "Assembly-CSharp", "YourMoneyClass");
if (!cls) continue;
// A) static singleton field, try the usual names
const char* names[] = { "Instance", "_instance", "instance", "Current" };
for (const char* nm : names) {
FieldInfo* f = il2cpp_class_get_field_from_name(cls, nm);
if (!f) continue;
void* inst = nullptr;
il2cpp_field_static_get_value(f, &inst);
if (inst) { g_wallet = inst; return g_wallet; }
}
// B) scene search, only valid for MonoBehaviour-derived classes
Il2CppClass* objCls = il2cpp_class_from_name(
il2cpp_assembly_get_image(asms[0]), "UnityEngine.CoreModule", "Object");
const MethodInfo* m = il2cpp_class_get_method_from_name(objCls, "FindObjectOfType", 1);
if (m) {
Il2CppType* t = il2cpp_class_get_type(cls);
void* args[1] = { &t };
Il2CppException* ex = nullptr;
Il2CppObject* o = il2cpp_runtime_invoke(m, nullptr, args, &ex);
if (o && !ex) { g_wallet = o; return g_wallet; }
}
}
return nullptr;
}
Then your button is trivial, and the offset stays as is:
Code:
void OnClickAddMoney() {
il2cpp_thread_attach(il2cpp_domain_get()); // only if this runs off the Unity main thread
void* inst = ResolveWallet();
if (!inst) return;
((void(*)(void*, int))getAbsoluteAddress("libil2cpp.so", 0x19B0110))(inst, 1234);
}
2. Pending flag drained by a per-frame hook
No reflection at all. Your button only raises a flag, and a method that already has a valid instance consumes it. Hook something on the same class that ticks every frame (
Update, a UI refresh, a timer) rather than
ConsumesMoney, so the money lands immediately instead of waiting for a purchase.
Code:
static std::atomic<bool> g_pending{false};
void OnClickAddMoney() { g_pending = true; }
void (*_Tick)(void* instance);
void Tick(void* instance) {
if (instance && g_pending.exchange(false))
((void(*)(void*, int))getAbsoluteAddress("libil2cpp.so", 0x19B0110))(instance, 1234);
_Tick(instance);
}
This is the most robust version on arm64 and it survives offset drift on the add function, but it depends on a ticking method existing. If the class has none, go back to option 1.
3. Static wrapper
Some games ship a static convenience method that resolves the singleton internally (
AddMoneyStatic,
AddMoneyById, a PlayerData helper). If one exists in dump.cs you do not need an instance at all, just call it. Grep the class for static methods taking an int before you write any of the above.
Gotchas that will bite you
- Thread affinity. A menu click handler usually runs on the render thread, not the Unity main thread. Any il2cpp call there needs
il2cpp_thread_attach(il2cpp_domain_get()) first, or the engine asserts. Attach once per thread, not per click.
- Do not run
FindObjectOfType on every click. It walks the scene and stutters. Resolve once, cache, and re-resolve only when the cached pointer comes back null.
FindObjectOfType only returns active scene objects. If your money class is a plain C# class and not a MonoBehaviour, scene search will never find it and option 1A or option 2 is your only route.
- Offsets move between builds. 0x19B0110 and 0x19B0324 are for one specific version, so re-dump before assuming they hold.
- If the UI reads from a cached copy of the value, adding money changes the model but not the label until the next refresh. Call the refresh too, or the player sees nothing happen.