Problem setting boolean in "OnMouseDown"

Hello together, I am pretty new to unity and coding in general and right now I feel pretty stupid, never had this kind of problem :smile:

My goal is to execute the code in “OnMouseDown” once, so I am simply checking for the bool “equip” in an If-Statement and setting it to the opposite to prevent from executing it again.

Why the hell does the code gets executed over and over again when I am clicking on the object that contains this script? The If-statement is working, but it seems like that I cannot set the bool to true.

Im thankful for every respond ! :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pickUpWeapon : MonoBehaviour
{
    private GameObject character;
    private Vector3 offsetPistol = new Vector3(0.5f, 0.4f, 0.3f);
    private Rigidbody rb;


    private bool equip = false;
    void Awake()
    {
        rb = GetComponent<Rigidbody>();
        character = GameObject.Find("Player");
    }

    void OnMouseDown()
    {
        if (equip == false)
        {
            equip = true;

            GetComponent<BoxCollider>().enabled = false;
            rb.useGravity = false;
            rb.freezeRotation = true;

            this.transform.position = character.transform.position + offsetPistol;
            this.transform.parent = GameObject.Find("Player").transform;
        }
    }
}

Did you add a Debug to see if the code in the if statement is running over and over again?

If you do something like

Debug.Log("OnMouseDown", gameObject);

then when you click on the debug message it will also flash the gameObject that is spitting out the debug message. Just to make sure it’s the same object.

Otherwise, your script should have no issues setting the bool to true, assuming you aren’t hitting any other errors.

Hello Brathnann, thank you so much for your response! :slight_smile:

I created this topic after 3 hours of trying to find my mistake, after that I went for a long walk and sat down again… found my mistake after 10min :sweat_smile::sweat_smile:

I assumed that when I put this script on a prefab and objects get created from that prefab, that this is the same script which exists only 1 time. But of course every created object got its own script and the bool value doesn’t change in the new created object(script) when I click on the previous created object…:sweat_smile:

Maybe I should have mentioned that I am using the script on prefabs, sorry for wasting your time, thanks a lot mate! :slight_smile:

Greetings
Nanachi

1 Like

No worries, glad you have it figured out.

1 Like

Glad you sorted it out. Probably what you need is persistent-for-the-game storage of the fact that you picked up this weapon at all this game, such that you would no longer keep a local boolean but make the script instead respect the persistent boolean.

If you want simple persistent storage for the lifetime of a single game, here are some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

https://pastebin.com/SuvBWCpJ

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

https://pastebin.com/cv1vtS6G

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}
2 Likes

Thanks for the response, Kurt-Dekker! :slight_smile:

I haven’t heard of singletons yet, thank you for bringing it up.

As a quick fix for my problem, I created a script and assigned it to an object, which will exist at all time and never gets destroyed or something (plane I use as my ground).
In this script I just declared a “public static” variable which I can access from my objects I mentioned above.

I think the key idea of a singleton is the same somewhere, probably with some advantages, I’ll keep your suggestion in mind, might come in handy someday :slight_smile:

Greetings
Nanachi

1 Like