How to avoid creating the GameObject on screen? - unity3d

I am instantiating enemy(Clones) from an enemy GameObject in Unity using c#. I have written a destroy function to delete only the enemy(Clones) when the bullet hits them. Unity renders the enemy GameObject on screen.
I cannot destroy this GameObject in runtime since unity will not instantiate any more enemy(Clones). Please help me not render the base enemy GameObject as it serves no purpose on screen.
Note: Enemy is scripted to walk towards player position and hence keeping the object outside the screen is pointless.
Please Help
here is a snippet of my code. I dragged the enemy GameObject into the Project section. Now it has created 2 Enemy objects on screen
public Rigidbody2D enemy;
void enemySpawn()
{
Rigidbody2D enemyInstance;
enemyInstance = Instantiate(enemy, new Vector3(Random.Range (2,8), Random.Range (-4,4), 0), Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
}

Make your enemy into prefab this can be done by dragging the gameObject into projectview.
You can instantiate new enemies from the prefabs with any gameObject. For example an empty one without any rendering components.
Instantiate can be done with this kind of script:
public GameObject enemy;
void InstantiateEnemy () {
GameObject enemyClone= (GameObject) Instantiate(enemy, transform.position, transform.rotation);
}
Just set the public GameObject in the editor and call the InstantiateEnemy function from somewhere.

Related

Weapon Pick-Up and shoot

I'm new to Unity and I was wondering if anyone knows how to make the player pick up a weapon and shoot it in Unity 2D. I have already made the sprite for the gun and I've been trying to pick it up with this code:
public GameObject player;
void Update() {
void OnTriggerEnter2D(Collider2D col) {
if (col.tag == "Player") {
gameObject.transform.position = new Vector3(player.transform.position.x + 2, player.transform.position.y, player.transform.position.z);
}
}
}
The gun is set as a trigger and has no rigidbody. All the youtube tutorials show how to pick up an object and then destroys it, but don't show how to "hold" the object.
If you do player.transform.position.x + 2, that sets the gun to the x position of the player + 2. This is not a good way of doing it.
My advice is to do it this way instead.
Copy the gun.
Parent the copy to the camera
Place the gun somewhere on your player.
Disable the gun
In your player script add this line
public GameObject weapon;
Now, in your weapon pickup script, change the player reference to whatever your player script is called instead of a GameObject.
For example, lets assume your player script is called PlayerScript.cs, instead of
public GameObject player;
You should do
public PlayerScript player;
Now, we have a reference to the player script. We can now access the weapon.
Change the OnTrigger to the following:
void OnTriggerEnter2D(Collider2D col) {
if (col.tag == "Player") {
// Set the weapon active
player.weapon.SetActive(true);
// Destroy this pick-up
Destroy(gameObject);
}
}
Also, in your code you have ontriggerenter inside the update method. That is not how it works. Make sure the method is outside of the update method.
Now when you touch the gun on the floor. It should now enable the gun in the players hand and destroy the one on the floor.
In the editor, drag the players gun to the weapon field in the player script.
Also, drag the player to the player field of the gun on the ground.
Hope this helps, feel free to ask more questions if this doesn't work. Keep us updated. :)

Get Prefab in the script of an Instantiated Object - Unity 2D

I am making a space invaders game, and have a script attached to a separated gameObject that instantiates Enemies. I can drag in the prefab and make it work, but then I want the enemies to spawn bullets themselves, with the bullet being a prefab.
I have started with just creating a variable for the bullet, but how would I access the prefab in script, as if I just drag it into the prefab, it won't be useable after the prefab is instantiated.
GameObject bullet;
void Start() {
bullet = // Insert Function to access prefab
}
Thanks
If you put your prefab in a Root Folder named "Resources" you can load prefabs from there like this:
Instantiate(Resources.Load("YourBulletPrefab"));
But you can also make the variable public...
public GameObject bullet; // or use private + [SerializeField]
...and then in Inspector, drag your Prefab in. No need to initialize it in Start() in that case.
I usually solve this by adding a public GameObject bullet variable to the "enemy" script AND the "enemy spawner" script. In the inspector you give reference to the "enemy spawner" to access the bullet prefab. And then, when you spawn an enemy, you pass its reference to the enemy. Something like this:
Enemy script:
class Enemy
{
public GameObject bullet;
private Shoot()
{
//Shoot bullet
}
}
EnemySpawner script:
class EnemySpawner
{
public GameObject enemy;
public GameObject bullet;
private Spawn()
{
GameObject newEnemy = Instantiate(enemy);
enemy.GetComponent<Enemy>().bullet = bullet;
}
}
By doing so, you will be able to access the bullets later as well.

How to detect collision between moving rigidbody cube and static empty object?

I am new to unity and working on collision detection.
I have a rigidbody cube and an empty object with a cube mesh. The rigidbody cube moves with arrow keys and the empty object is static. Both have a box collider.
How do I detect collision between this empty object and the rigidbody cube?
I am wondering whether it should be OnCollisionEnter or OnTriggerEnter and how to use the correct command.
Thank you for your help.
First you need to set the box collider component to the cube and to the empty object with cube mesh. Then you can use this code:
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
public GAME_MANAGER GameManager;
private void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Obstacle")
{
movement.enabled = false;
}
}
}
Make sure you have set the tag of the obstacle to “Obstacle” and add this script to the player or the cube. Here,
movement.enabled = false;
You can apply you own logic according to your need.
Thanks

Instantiate doesn't work in unity with navmesh

I'm having some problems on instantiating an enemy in unity, it gives me always this error setDestination" can only be called on an active agent that has been placed on a NavMesh., and then it places a 2d model somewhere in my map
public GameObject enemy;
public Transform spawn;
// Start is called before the first frame update
void Start()
{
spawnNew();
}
void spawnNew() {
Instantiate(enemy, spawn.transform.position, Quaternion.identity);
}
After you have spawned an enemy, call the NavMeshAgent.Warp() function to position the agent on the desired position, which of course must be inside the navmesh.

How to repeat collision in unity?

I have a bird and a flower if the bird collides with the flower, the flower Destroys and another flower grows, else the flower drops and the bird goes away.
The problem is the newly created flower doesn't collide with the bird and just drops. how can I solve this?
public SpriteRenderer bird;
public SpriteRenderer flower;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("bird"))
{
Destroy(this.gameObject);
float x = Random.Range(-8f, 8f);
Instantiate(flower, new Vector3(x, 6, 0), Quaternion.identity);
}
else if (collision.gameObject.CompareTag("flower"))
{
Destroy(bird);
}
}
You're instantiating the flower using Spriterenderer instead of a GameObject. Try changing public SpriteRenderer flower; to public GameObject flower; and create a prefab out of the flower Object.
If you don't know how to create a prefab, all you need to do is drag a GameObject from your scene to an assets map. If this is still unclear to you, here is a reference to the Unity documentation: https://docs.unity3d.com/Manual/CreatingPrefabs.html
Once you've created the prefab you can drag the prefab onto the script into the flower GameObject. Then it should work.
If you have more questions make sure to ask them!