Why does my two spawned 2D prefabs not interact with eachother?

As soon as I press spacebar, the game script creates a prefab called “laser” which is the projectile shot by the player (yellow cross). Additionally, I have a script that creates multiple lines of enemies through a duplicated prefab, this enemy is called: “Invader” (red devils). But why doesn’t the “laser” react with the “invader” when touched? The “Invader” is neither destroyed nor do I get a print message in the console. Strangely, however, the laser collides successfully against the level border, maybe because it was not spawned as a prefab but was already there from the beginning?..

7981911--1024959--Screenshot 2022-03-21 170711.jpg

I recorded all my inspector settings, my scripts and my gameplay here:

What do you mean by “interact” and “react?” There is no interact or react API.

There are triggers, colliders, collisions, etc. and all those are well documented in terms of requirements.

You can do other things in code such as check distance and decide what you want to do.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

A few things:

  • Don’t modify the Transform when using 2D physics, that’s the role of the Rigidbody2D. Use its API to cause movement.
  • At 50 seconds in your video, you have a large warning in the BoxCollider2D indicating that no physics shapes were created because of bad scaling etc. Sure enough, your box size is 0.0001 and transform scale is 0.3 so you’re trying to create a box with sides of 0.00003 m (30 micrometers in size).
  • Turn on gizmos if you’re expecting to see colliders. You’d soon see that there are no colliders being shown. You can also turn them all on permanently while debugging by going into “Project Settings > Physics 2D > Gizmos > Show Colliders”.
1 Like

Just in the static video screenshot above I can see a Z scale that is zero. Zero scale is ALWAYS to be avoided. It will actively cause all kinds of problems with lighting, layout, anchoring, and probably even view frustum culling for all I know (just guessing).

As for the micro-sized colliders Melv notes above, be sure you never rotate a 2D collider in any other axis than Z. This can happen when you try to put a 2D collider on (for instance) a 3D shape imported from Blender3D. When you import anything from Blender, its rotation is (-90,0,0) and if you slap a 2D collider on that, it will be turned edge-on and have effectively zero size, and Unity should throw up one of those warnings. To fix, rearrange your Prefab hierarchy so the root object is NEVER rotated except around Z and put the collider there, then child other objects below it that do rotate in X or Y axes.

2 Likes

What Warning? There is no warnings…

Melv tells you the actual time. Go there. Look for warnings. Here’s a screenshot:

7984467--1025595--Screen Shot 2022-03-22 at 7.41.48 AM.png

1 Like

I turned every Z scale to one, nothing changed.
I activated “always show colliders” in gizmos, and I found out that the invaders dont show a collider. but they have one, so I took a closer look and saw that with the “0.00003 m”. after I turned them to the size of the sprite ( 2.6 & 2.6) and finally, the laser got destroyed after reaching the invaders!
Thank you so much! - I desperated for days, just to find this little issue…

Whilst Z scale of zero does affect a bunch of things, it won’t affect 2D colliders but what Kurt said still stands, never have a scale of zero like that.

If the big exclamation mark and then me saying the exact time in the video wasn’t enough then I’d say you need to take a break! :wink:

Note that you can check for this too by looking at the Collider2D in the inspector, opening its “Info” fold-out and looking for “Shape Count”. That’ll likely be zero meaning it didn’t produce any shapes. You’ll get a warning if any shapes are removed.

Glad you got it working.

I thought in the Unity Console or in Visual Studio?

In YOUR video. :slight_smile:

I said:

“At 50 seconds in your video, you have a large warning in the BoxCollider2D indicating that no physics shapes…”

Warnings and errors can come from ANYWHERE.

That’s why Melv gave you a timestamp (0:50).

That’s why I gave you a screenshot.

At this stage, I’m not sure how much clearer I can make it.

Errors can literally happen ANYWHERE.