On collision, enable script. Doesn't work.

Hey,

I use this script, so when my player hits the grey square, another script from another object is activating. But it doesn’t work. The script that has to be activating is called ‘‘RandomSpawn’’. I deactivate it, because otherwise the script is activated when I start the game.

public GameObject player;


    void OnCollisionEnter (Collision col)
  
    {
        if(col.gameObject.name == "GreySquare")
        {
  
            gameObject.GetComponent<RandomSpawn>().enabled = true;
        }
    }

if you are using 2d colliders, then you should use: void OnCollisionEnter2D (Collision2D col)

and be sure, both objects have collider 2D, and one have rigidbody 2d (you can add it to gray square and set it to kinematic)

Also be sure that your object layers can collide each others (see into Edit > project settings > physics 2D).

Also, it’s a bit unclear whether this script is attached to a player, or to some other object. If this script is attached to player. is RandomSpawn script also attached to player, because that is what your code looks like?

This is attached to the player. The randomspawn is attached to another object and if you hit the GreySquare the randomspawn script enables

I will test the other comments out. ty

I made a new script and used a trigger instead of using collision, but it doesn’t seem to work. I attached this script to ‘‘GreySquare’’ and I did what you did ‘‘and be sure, both objects have collider 2D, and one have rigidbody 2d (you can add it to gray square and set it to kinematic)’’. Also my player really kind of lags right now.

My script:

    public GameObject SpawnLine;

    void OnTriggerStay2D (Collider2D other)
    {
        Debug.Log ("Het werkt :)");
        gameObject.GetComponent<RandomSpawn>().enabled = true;
    }
}

Thnx.

Its on

https://gyazo.com/d4e500f455a847453ac98fe79121b3c7

is gray moving object ? Add rigidbody2d to static object. Do you have enabled trigger checkbox on one object in editor ?

and you can try check collision with Unity - Scripting API: Physics2D.OverlapCircle
so you dont need rigidbody (if it is realy laggi)

Unity has a quick cheat sheet to check which objects produce a physical collision (colliders) or collision event (triggers) :smile: (scroll to the bottom). There’s also some great info about Static vs. Rigidbody/Rigidbody Kinematic Colliders :slight_smile: Hope that helps!!!

  • No
  • Yes the greysquare has a rigidbody2d with kinematic.
  • Yes

How would a script look like? It looks really complicated. And why does my method not work for me.
Physics2D.OverlapCircle

put a bool on your spawn script & set it to false.
When your player hits the grey box access the spawn script & set the bool to true.
In the spawn script Update use an if statement so the spawn only works if the bool is true.
In that if statement, after the spawning has finished, set the bool back to false so it stops spawning.

I will try this out, thanks.

Hey,

I made my script like this, the script does turns off, that works. But it doesn’t turn on.

void Start ()
    {
        gameObject.GetComponent<RandomSpawn> ().enabled = false;
    }

    
    void Update()

            {
                if (gameObject.GetComponent<RandomSpawn> ().enabled = true);

            {
                int randomNumber = Random.Range (0, positions.Length);
                transform.localPosition = positions [randomNumber];

                if (transform.position.x > -3 && transform.position.x < -2) {
                    transform.rotation = Quaternion.Euler (0, 0, 90);


                }

            }

        }

}

I also get this error:
https://gyazo.com/da53ee00dcc930fd4c534ff6ca27731a

This is my script on my ‘‘GreySquare’’ (that is where the error came from)

void OnTriggerEnter2D (Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            gameObject.GetComponent<RandomSpawn> ().enabled = true;
            Debug.Log ("It should collide now");
        }
    }
}

I tagged my player with ‘‘Player’’.

The ‘‘GreySquare’’ has a rigidbody 2d with kinematic and a box collider 2d, with ‘‘is trigger’’ turned on.
The ‘‘Player’’ has a box collider 2d only.

I hope to hear from you soon.

Thanks

Have you tried putting a breakpoint on the line that is setting the bool to true so you can check that it is finding it to update it? If it isn’t then there is an issue with the way you are trying to pass that across.
Also, why are you doing get component to set the bool on the spawner? If the spawn script is on the spawn point then the spawn script only néeds to have the bool without all the get component stuff.

Sorry but I don’t understand what you mean with this?

Sorry, haven’t said that I think, but the spawn script is on the spawn object (the line), it isnt on the ‘‘GreySquare’’

If the spawn script is on the spawn object then it doesn’t need the get component to set the bool to false since the script is on that component.
A breakpoint is a debug tool. You set it on the line you think is causing the issue then run the game while using the debugger. It can be slow as it needs to load everything so give it a chance before you panic. The game will then run & when it goes to action the line/s with break points it will stop & flick to the code & by hovering the mouse over variables etc you can see the values before it runs, you then step into the code & see what the value is after the line runs. If your breakpoint is never called then you know your code has an issue as it isn’t actually processing your line.

http://docs.unity3d.com/432/Documentation/Manual/Debugger.html

If I don’t need to use ‘‘get component’’, how should I do it than? Shouldn’t it work with this?

I think once you turn a game object off it isn’t there any more to be found to turn back on. Research using break points & put one in at the collision & see what happens when it tries to find the component to turn it back on.

Can someone pls help me with this.

I actually think the issue is caused by enabling/disabling the object. Leave it always on & just use a bool on your spawner that is set to false, turns to true when the player hits the grey box & turns back to false after it has spawned however many items you want.