0

What I have right now are two boxes been moved by the same script, it happens if the boolean canMove, which is in the script, is true.

The problem I have is that I want to turn canMove to false for just one of those GameObjects. But when I do that, obviously, it changes for both GameObjects.

What could I do to have control on this variable on each GameObject?

6
  • Hi. Can you post the part of the code that contains the boolean? Commented Mar 1, 2019 at 5:33
  • is the script attached to each gameobject ? Commented Mar 1, 2019 at 5:34
  • @Reasurria Hi, I don’t have the script right now, but it’s just a private bool canMove with an OnTriggerEnter and OnTriggerExit that make the Boolean false and true, in that order. Commented Mar 1, 2019 at 6:35
  • @Xamassassin Yes, is just one script attached to both GameObjects. Commented Mar 1, 2019 at 6:36
  • Please don't add [Solved] to the title. The question will already visibly marked as solved once an answer is marked as accepted. Commented Mar 1, 2019 at 11:26

2 Answers 2

1

If I understand you correct the issue is you have a script lets say like

public class CollisionHandler : MonoBehaviour
{
    public bool CanMove;

    void OnTriggerEnter(Collider other)
    {
        CanMove = false;
    }

    void OnTriggerExit(Collider other)
    {
        CanMove = true;
    }
}

and you want to set it to false only to one of the objects, right?

Maybe a bit hacky but you could do

public static List<GameObject> lastReset;

void OnTriggerEnter(Collider other)
{
    //get your own index in list
    var myIndex = lastReset.IndexOf(gameObject);

    //get others index
    var otherIndex = lastReset.IndexOf(other.gameObject);

    // Only go on if other is exactly the object in front of you
    if(otherIndex != myIndex - 1) return;

    // You way to define if can move
    CanMove = ...;
}

Than wherever you do your resetting also do

CollisionHandler.lastReset.Remove(objectYouReset);
CollisionHandler.lastReset.Add(objectYouReset);

To remove it and set it to the end of the list.

Sign up to request clarification or add additional context in comments.

7 Comments

That’s what I want, but I didn’t mention something I thought wasn’t relevant, but now I see it is. I want one of the objects to set canMove when it collides with the other object that has the same script. What I was trying is to use collider.bounds to say that when the OnTriggerEnter is called, if the bounds.max of the box A is less than bounds.min of box B box B stop moving so the two objects don’t overlap. The problem is that the script is deactivating both GameObjects movement.
But for which object do you want to deactivate it? And you can do the same for OnTriggerEnter. And you can add whatever check you want in order to "filter" some condition
Let me explain you, these two boxes are moving one behind the other, and there is a 3rd game object that when collides with each box send them back to the starting point, creating a loop. The problem is that one of the boxes is bigger than the other, so when these are sent away, they are overlapping, and that’s what I don’t want. So, having that in mind, it is not defined which of the boxes is going to deactivate the other, all We know is that one of those is going to deactivate the other.
Isn't the one that should be stopped allways the one that was reset last?
in this case, it is, but I want it more generic because I’ll add more boxes later so the first deactivates the second, the second the third and it goes on...
|
0

You can make bool variable public so that you can see them in inspector. Now change the true for one and false for other in Inspector.

1 Comment

Yes, the problem is that I want the Boolean to change depending on wether it is colliding with an specific object or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.