set bool true/false with collision

hey I’m trying to check if a gameObject is already instantiated with collision.
so I made a bool like this:
(tried a dozen different versions, like collision instead of trigger, or stay instead of enter, and so on)

	void OnTriggerEnter(Collider other){
		if (spotTaken == false){	
			if (other.CompareTag("block")){
				spotTaken = true;
			}
		}
	}
	void OnTriggerExit (Collider other){
		if (spotTaken==true){
			if (other.CompareTag("block")){
			spotTaken=false;
			}
		}
	}

also tried:

	void OnTriggerEnter(Collider other){
		if (spotTaken == false){	
			if (other.CompareTag("block")){
				spotTaken = true;
			}else{
				spotTaken = false;
			}
		}
	}

well i won’t start posting everything i tried, bottom line is i get it to set true but not false again
then the other part is the instantiating i got also lots of different versions like:

(Input.GetMouseButtonDown(0)) {
           	if (Physics.Raycast(ray, out hit)){
				if (hit.collider.CompareTag("block")){
					Destroy(hit.transform.gameObject);
					collisionScript.spotTaken = false;
				}else if(collisionScript.spotTaken == false){
                	Instantiate(levelBlock[blockID], spawnPos, transform.rotation);
				}
			}
		}

or even this simple line to check if it would only instantiate if the bool is false

		if (collisionScript.spotTaken == false  Input.GetMouseButtonDown(0)){
			Instantiate(levelBlock[blockID], spawnPos, transform.rotation);
				
		}

so i press play, the bool is false and i instantiate a block, then the bool is true but it doesn’t matter the instantiating keeps happening on mousedown…

all i want is to get the bool true on entering collision and false when no collision so i can check if there is already a block and if so it won’t be instantiating an other one at the same place.
the collision script is placed on a small ball the snaps to only int’s in vector3 space.
I’m confused with all the trying and can’t see the forest through the tree’s anymore…
(the ray is for the mouse to delete one with the mouse)

made a bit of a mess off this post sorry for that…

You dont ask if a bool is false. Just say, if(collisionScript.spotTaken){} else Instantiate(bla bla);

Have you tried assigning the instantiated block to a var then checking if the var is null?

if i do it like this it won’t instantiate anymore?

	if (Input.GetMouseButtonDown(0)) {
           	if (Physics.Raycast(ray, out hit)){
				if (hit.collider.CompareTag("block")){
					Destroy(hit.transform.gameObject);
					
				}else if(collisionScript.spotTaken){
                	Instantiate(levelBlock[blockID], spawnPos, transform.rotation);
				}
			}
		}

hmm it shouldn’t be part of the ray if, right?
didn’t work either:

		if (Input.GetMouseButtonDown(0)) {
			if(collisionScript.spotTaken){
                	Instantiate(levelBlock[blockID], spawnPos, transform.rotation);
				}
           	if (Physics.Raycast(ray, out hit)){
				if (hit.collider.CompareTag("block")){
					Destroy(hit.transform.gameObject);
					
				}
			}
		}

@Black mantis; no good idea will try that in the mean time

The trigger and collision events won’t work because Raycasts don’t trigger them. Assuming spotTaken is a static bool inside a script called collisionScript you would want something like this.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.tag == "block")
            {
                if (collisionScript.spotTaken)
                {
                    Destroy(hit.gameObject);
                }
                else
                {
                    Instantiate(.....);
                }
                collisionScript.spotTaken = !collisionScript.spotTaken;
            }
        }
    }
}

Hah…You missed the “else”. else if(collisionScript.spotTaken){}else{ :stuck_out_tongue: I’m pretty sure this is your problem. Trust me, I’ve never personally understood why asking if a bool is false doesnt work. If you ask if a bool is false, the statment after will always happen… So this is why i always as if its true, followed by ELSE.

but I’m using the ray from the mouse to destroy GO’s and the red dot’s collision to instantiate GO’s

not the best method probably
maybe i better use only the red dot to create and destroy

You can Try this way:

var spotTaken : boolean = false;
function OnTriggerEnter(other : Collider)
{
    if (spotTaken == false)
    {    
     if (other.gameObject.tag == "block")
        {
            spotTaken = true;
            print ("Work?");
        }       
    }
 
}
function OnTriggerExit(other : Collider)
{
    if (!spotTaken==true)
    {
        if (other.gameObject.tag == "block")
        {
            spotTaken=false;
        }
    }
}

work one time, after you collide once whit the box whit that tag name, if you pass hover again nothing appens:. that mean you have already pass it:)

Hope help you:

ok so i deleted all the collision stuff and got my ray drawn from the red dot instead of the mouseposition, so i want to instantiate and delete with the ray but for some reason it still doesn’t work :rage:
it won’t instantiate?

Vector3 forward = -(clone.transform.TransformDirection(Vector3.forward) * 10);
		ray =  new Ray(clone.transform.position, forward);
		Debug.DrawRay(clone.transform.position,forward , Color.red);
		if (Input.GetMouseButtonDown(0)){
	        if (Physics.Raycast(ray, out hit)){
	            if (hit.collider.CompareTag("block")){
	                Destroy(hit.transform.gameObject);
	            }
	            else{
	                Instantiate(levelBlock[blockID], spawnPos, transform.rotation);
	            }
	        }
	    }

EDIT ah ok stupid mistake should have been this:
Vector3 forward = (clone.transform.TransformDirection(Vector3.forward) * 250);

but now I’m back at the same problem, I’m spawning loads of block at the same position :frowning:
could taht be because I’m inside the block with the dot and thus the ray?

arg the humanity!!! totaly forgot this
Note: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour.

anyway around it?

or is it back to collisions then?

last bump :wink:
solved gave the ray an offset off -5 on the z axis :slight_smile: