This has been solved. I deleted and recreated the buttons that were causing my issues. After creating the new ones, my issue went away. I am not sure what could have caused irregular button behavior with the previous buttons.
Hello,
I have a class with some methods in it that I call from buttons on a Canvas–>Panel–.>HorizLayoutGroup.
When I call the method directly from a script it works as intended. When I call it from the buttons (Button - TextMeshPro) it turns every variable in the method to 0 (int, float,etc), or (0,0,0) in the case of a Vector3. Those same buttons also call a method to close the menu, which works fine, I am assuming because there is not data to manipulate.
So I am at a loss. Clearly the buttons have access to the script and methods since all the other ones work. I have created new global private and public variables in the script purely for testing so I know they are not being altered anywhere else. And finally I also can see that even though my debug.log shows the variables are 0, at runtime in the inspector I have exposed them and they read the correct values. I have also tried creating other methods to call from the buttons with variables and they all fail the same way.
The method that is failing is: public void BuildSelectedTower(int choice)
I am stumped. Here is my relevant code. Would love some help. Thanks so much!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuildableMenu : MonoBehaviour
{
public Vector3 towerBuildPosition = new Vector3(2, 2, 2); //Assignment just for testing to see the values change.
public Vector3 assignmentCheck;
void Start()
{
Debug.Log(towerBuildPosition); //valid
//This works as intended
GameObject newTower = Instantiate(GameManager.instance.availableTowers[0], new Vector3(2, 2,2), Quaternion.identity);
}
void Update()
{
// Check for left mouse button click
if (Input.GetMouseButtonDown(0) && LevelUI.instance.towerSelectionPanel.activeInHierarchy == false)
{
// Cast a ray from the mouse position in 2D space
Vector2 raycastPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(raycastPosition, Vector2.zero);
// Check if the ray hits a GameObject with the "Buildable" tag
if (hit.collider != null && hit.collider.CompareTag("Buildable"))
{
//Set the location for building
towerBuildPosition = hit.collider.gameObject.transform.position;
assignmentCheck = towerBuildPosition; //Testing var, shouldn't be needed after solution is found
Debug.Log("The position of the collider is: " + towerBuildPosition); //valid
Debug.Log("In the family is is: " + towerBuildPosition); //valid
// Open the menu for the buildable object
OpenTowerChoiceMenu();
}
}
if (Input.GetMouseButtonDown(1))
{
OpenTowerChoiceMenu();
}
}
//This method changes or something has changed assignment check to (0,0,0) by the time it gets here in execution.
public void BuildSelectedTower(int choice)
{
Debug.Log("In the family is is 2: " + assignmentCheck);
GameObject newTower1 = Instantiate(GameManager.instance.availableTowers[choice], assignmentCheck, Quaternion.identity);
CloseTowerChoiceMenu();
}
public void BuildSelectedSupportTower(int choice)
{
}
public void OpenTowerChoiceMenu()
{
LevelUI.instance.towerSelectionPanel.SetActive(true);
}
public void CloseTowerChoiceMenu()
{
LevelUI.instance.towerSelectionPanel.SetActive(false);
}
}
The TMP button in use, I have multiple, all the same other than the passing parameter.
