- Home /
Question by
unity_89C37FB55DBF54395314 · Aug 05, 2021 at 04:09 AM ·
booleans
Why is my boolean value not passing to a different script correctly?
For some reason, I can't pass my boolean from one script to another. In one script, I set it to true and then I debug.logged it and it sent true back, however in the other script it is still set to false. Code: Script 1:
public void OnMouseDown()
{
if (!hasObject)
{
newDrag = Instantiate(DragObjectCard);
newDrag.transform.position = new Vector2(ObjectCard.transform.position.x, ObjectCard.transform.position.y);
newDrag.transform.position = Input.mousePosition;
hasObject = true;
Debug.Log(hasObject);
gameManager.draggingObject = newDrag;
}
}
Code 2:
public CreateObjectDrag OD;
public void OnMouseDown()
{
Debug.Log(OD.hasObject);
if (OD.hasObject == true)
{
Debug.Log("sup");
gameManager.PlaceObject();
gameManager.draggingObject = null;
Destroy(GetComponent<CreateObjectDrag>().newDrag);
}
}
I am referencing an object from the prefabs "GameObject" or "OD" which has the first script "CreateObjectDrag"
Comment
Pass a debug.log for the Od.hasObject boolean in the second code and check what it returns, if it is false....it means that some other piece of code is constantly changing it's value to false
Your answer
![](https://koobas.hobune.stream/wayback/20220613041224im_/https://answers.unity.com/themes/thub/images/avi.jpg)