Unity: My enemy prefab can't find the player on the scene - unity3d

So my problem is, that I have a GameObject, which spawns enemies, and it has a variable, where I can put my enemy prefab. My only problem is, that my enemy doesn't follow the player.
I made an enemy prefab, with the A* path-finding algorithm, I included the AIDestinationSetter also. The A* works just fine when my enemy is in the scene, but when I try to spawn it, it somehow doesn't seem to know what to do. Any ideas what is wrong?
Thanks for all help, much appreciated!

If the problem is setting the target as the player prefab instead of the player in the scene, add a tag to the player and in the script, get the reference of the player in the scene using
target = GameObject.FindWithTag("Player");
Note: Works only if there is only one game object with the tag "Player" in the scene and if there are multiple of them, this method returns the first one it finds.
To get multiple players as targets, use
targets[] = GameObject.FindGameObjectsWithTag("Player");
For more information, refer:
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

Related

Child Components with rigidbody

Context
I have in my project two elements. A player (just a cube) and some eyewears.
When the glasses aren't attached to the player, i want them to have properties of a Rigidbody. But when the eyewears are attached to the player, i want them to be a static object, so the player can process the collisions and physics.
I have tried:
DestroyElement(rigidbody) when the player pickup the eyewears. When he leaves them, i recreate the rigidbody with AddComponent
It worked nice, but in the future other elements will be attached and they will not share the same properties of rigidbody. I though maybe i could save the rigidbody instance, so when the player leaves the glasses i assign it to them. I could't.
AddComponent don't accept arguments.
Then i tried to set "kinematic mode" when my player wears the eyewears. It didn't go well, my player can't jump anymore and sometimes he glitches in the floor.
How can i resolve this?
GameObject.AddComponent does take an argument, or a generic argument (preferred):
go.AddComponent<RigidBody>();
this is also possible, but deprecated, since you lose type-safety:
go.AddComponent(typeof(RigidBody));
However, RigidBody is not meant to be added/removed, and in your case I would say that kinematic mode is the way to go... but I can't tell why you're experiencing weird results with it.
Thanks to R Astra i checked again the eyewear collisions and i found out the problem. I had enabled the Convex mesh.
The col was causing trouble because the back meshes were inside the player
So, quickly i copied that eyewear and generate a new mesh. It worked!
Thank you very much!

Unity 5 // when enemy spawns , Enemy target is none?

I've got a basic AI script where I'll assign an object for the enemy to chase.
The problem is, when I turn the enemy into a prefab, the target assignment becomes blank and I cannot alter it while it's a prefab. I've tried to assign the object within the code itself but I'm not completely sure how to do so (I've tried a number of things but nothing has panned out).
Any tips on how to fix the first problem, or just how to assign a target within code would be very helpful. JavaScript would be the preferred language for code.
It's the intended behaviours of prefabs.
You can't link a gameobject which belongs to the scene, to a field of a prefab in your assets since it must be completely independant from any instance of your scene. Prefabs are intended to be instantiated, and then, you will be able to assign (through code) the public field you want to your instantiated enemy. See a prefab as a file in your HDD you can instantiate from.
Without any code it's hard to help, but I guess you can do something similar to this :
var newEnemy : EnemyAI = Instantiate(enemyPrefab);
newEnemy.target = GameObject.FindWithTag ("Player").GetComponent.<Transform>();
I'm not fluent at all with Unity script.
You are still able to "instantiate" the enemies directly in the scene by dragging and dropping the prefab into your scene, and you will be able to assign the Target of your instantiated prefab.

Unity Object Collision occurring without objects colliding

In my game my player avoid the obstacles and move towards the goal, but when i play the game in device its started colliding the game.
I tried to collide in editor but its not the issue anymore. here is the picture for detail info alt text
this is my player passing aside of an obstacle but in device its triggered the collision but its not colliding with the object.
What could be the issue, i really have no idea what's going on..
I am using OnCollision2D to check the collision.
Edit: Here is Rigidbody2D inspector

Unity- Colliding Passing Through

I made airplane shooter in Unity. First I make player and I already make all the code. And it works well. And then now I want to change the player object. I had already put the script and give same name and tag as the used player that I make. And the colliding is a mess. I can shoot enemies down. But the enemies can't shoot or hit me. The enemies bullet and the enemies passing through my player object.
I don't know why. Please help me to find the problem. Just tell me the possibilities of this problem cause.
Here is the screenshot of my new object component
Check if your Collider has the "Is Trigger" checked, that may be causing the error.

Issue with character models in unity

I am using the Unity 4 game engine and I have two scripts, one that tells the enemy to attack the player, and one that lets the player attack the enemy. The enemy can attack the player, but the other script does not work. If i apply the script to a game object such as a capsule or cube, the script works fine, but not when i apply it to an imported character model. The script basically works by checking the enemy's health and destroying the gameobject if it is equal to or below zero. I have a feeling it has to do with the character model not being an actual game object but i am pretty sure it is. Can anyone help?
When you make the enemy character. Make an empty game object. Then create your enemy inside of that object. In the hierarchy view it should be parented by the empty object. You can also do this by dragging the enemy to the empty game object in the hierarchy view. Then attach the script to the empty game object instead of the model.
Solution
Please check all the tags you are applying to the scripts .No Script could able to work until unless the tagging is done . The tagging help the game object to differentiate between normal object and the enemy so the tagging plays a vital role to run scripts .