New answers tagged

0

In the end, keeping the code from the question and changing the public field of the Prefab to type GameObject (not Transform) did the trick. (I did not need to use Resources.Load("PATH");) I hope this saves someone some time


1

For what I've seen in the comments your prefab is null because there is nothing assigned at runtime. Drag n dropping stuff in the inspector of your script is ignored by the engine at scene load. Once your script is assigned as component to a gameObject in your hiearchy then public slots will appear and you should do there your drag n drop. Otherwise you ...


1

object_index This read only variable returns the index of the object that the instance has been created from. So your code would read: if(object_index == obj_square) { // ... }


6

The best way to do this depends on a few things. I'm going to assume the following - if any of these are incorrect, please let me know and I'll update my answer: You want the closest enemy that is anywhere to the right of the player within a certain range, even if it is at a different height You want true distance, not just the one that is closest in ...


1

Assuming your enemies all have colliders, could you not simply fire a 2D Raycast https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html in a straight line to the right of your player and return the GameObject of the first collider it hits?


0

Made it work by assigning a "Left Side" tag to the enemies on the left and a "Right Side" tag to the enemies on the right. After that I find the closest enemy on a particular side by checking the distance from the enemy to the player and returning the closest one: private GameObject FindClosestEnemy(string side) { GameObject[] enemies = GameObject....


2

All the left enemy are in lower value in x-axis position. Objects on right side are on higher value. Subtract main character's x value from enemy's. Positive results are for enemies at right side.



Top 50 recent answers are included