-1

Update: It's the buttons themselves, not tempRefId, that are being swapped. Though the issue still persists.

I'm trying to write a mod for a Unity game, but I'm not sure this is a Unity related issue. In the game the player can open a map, and what this code does is add a button next to a few objects in the map. The issue I'm having is that the integer value associated with each button, tempRefId, is swapped every time the map is opened. It starts off correct, then is set to an incorrect value, then swaps back etc.. I am certain the code below is only run once, the first time the map is opened.

// Loop through a set of gameobjects in code above
if (child.gameObject.name.Contains("res-entry") && child.GetComponent<UIResAmountEntry>().refId != 0 && child.GetComponent<UIResAmountEntry>().valueString.Trim() != "0" && !child.Find("unique-string"))
{
    int tempRefId = child.GetComponent<UIResAmountEntry>().refId; // ID of resource
    Transform iconTransform = child.Find("icon"); 

    GameObject toggleHighlightButton = GameObject.Instantiate<GameObject>(
        original: iconTransform.gameObject,
        position: new Vector3(child.position.x+1f, child.position.y-0.02f, child.position.z),
        rotation: Quaternion.identity,
        parent:   child);

   UIButton uiButton1 = toggleHighlightButton.GetComponent<UIButton>();

   uiButton1.onClick += (id) => { ToggleVeinHeighlight(tempRefId, ref toggleHighlightButton); };
   uiButton1.onRightClick += (id) => { DebugStuff(tempRefId); };  // Why is this value inconsistant?
   toggleHighlightButton.name = "unique-string";
}

From my understanding an int is passed by value, so I don't understand how the value of tempRefId can change after assigning it, which probably means I don't understand my lambda expressions properly. If the issue isn't apparent from the code, what could be causing the issue?

The issue does not affect all instances of the created buttons, seemingly only the first and last ones created in the loop. The values of these two are the ones that swap each time the map is opened.

Ben
  • 460
  • 5
  • 18
  • 1
    Set a breakpoint on `UIButton uiButton1 = ` and inspect `tempRefId` has it changed? I have a feeling since you're creating a new `GameObject` with it's parent to `child` it changes the `tempRefId`. – Trevor Apr 13 '21 at 14:05
  • @Codexer I can't set a breakpoint, but by logging the value I can confirm that it doesn't change. – Ben Apr 13 '21 at 14:10
  • What does `ToggleVeinHeighlight` look like? If that's the case it doesn't change there, then maybe this routine is doing something with it? – Trevor Apr 13 '21 at 14:12
  • Unfortunately not, the value is only used as an array index in that method. – Ben Apr 13 '21 at 14:15
  • Are you using code injection? – Shadows In Rain Apr 13 '21 at 14:21
  • @ShadowsInRain Yeah, I'm using BepInEx – Ben Apr 13 '21 at 14:31
  • Please [DEBUG your code with breakpoints](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) .. why can't you set one? And in general: In lambda expressions variables can be [**captured**](https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp) which means in simple words even if is a value type in a lambda it is treated like a reference so if it is changed before the lambda is executed then you will get the new value instead .. it is pretty hard to tell without having a complete code – derHugo Apr 13 '21 at 14:34
  • Unfortunately, with code injection, anything may happen, including nasal demons™️. Can you try to extract your code (specifically part that declares variable) into separate function and tell if it changes anything? – Shadows In Rain Apr 13 '21 at 14:36
  • @derHugo I can't add breakpoints as I don't have access to a debug version of the game I'm injecting code to. As for capturing, that is why I added the temp variable, but I tried skipping the variable entirely and that didn't work either :( I did notice however that the issue only happens for certain buttons, I'll update the question. – Ben Apr 13 '21 at 14:48
  • @ShadowsInRain Unfortunately no change – Ben Apr 13 '21 at 14:48
  • 1
    Perhaps it's not `tempRefId` being swapped, but the buttons? Otherwise, what `ref` if for? – Shadows In Rain Apr 13 '21 at 15:00
  • @ShadowsInRain You're absolutely right, it is the buttons being swapped. Not sure why that's happening either but it's one step closer. – Ben Apr 13 '21 at 15:06

1 Answers1

0

The issue turned out to be that the child object I was using as a parent for my button was actually moving every time the map is opened, which seems to be a bug with the game. I fixed the issue by using the the parent object of child as the parent for my buttons, which doesn't move each time.

Ben
  • 460
  • 5
  • 18