- Home /
Hide gameobject?
Hi!
I have an enemy as a gameobject. I want to spawn this enemy at a set location. For this i have a spawningscript
using UnityEngine;
using System.Collections;
public class SwordsmanSpawn1 : MonoBehaviour {
public GameObject Swordsman;
public Vector3 spawnSpot;
void Start() {
Instantiate(Swordsman, spawnSpot, transform.rotation);
}
}
But i dont want the original gameobject of the enemy to be in the scene when i spawn it. When i start, there is 2 "swordsmen" one (the clone) in the desired spawning position and one (the original) in an undesired position. Is it possible to somehow hide the original gameobject so that only the clone will appear in the scene?
Thank you in advance
/Taegos
Answer by Wolfdog · Jun 26, 2015 at 03:34 PM
Just drag the prefab into a folder in Unity,
Delete the object from the scene (but leave the one inside the folder),
Make an empty gameobject in the scene and add your spawnscript to it.
Click on the newly created gameobject and drag-and-drop your prefab from your folder into the new gameobject's script's
Swordman
field.
Then you are good to go.
No, it solves the clone problem but now the clone "loses" its movement behaviour.
The movementscript of the enemy is:
using UnityEngine;
using System.Collections;
public class Zombie$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public Transform playerPos;
public float moveSpeed;
public float AttackAnimDuration;
private bool paused;
IEnumerator delay()
{
paused = true;
yield return new WaitForSeconds(AttackAnimDuration);
paused = false;
}
void Update () {
if (paused)
{
return;
}
float move = moveSpeed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, playerPos.position, move);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
StartCoroutine(delay());
}
}
}
The gameobject of the enemy now just moves to the 0,0,0 - position ins$$anonymous$$d of following the player. Every tag is correct and every gameobject is assigned.
Try this as your Zombie's movement script:
using UnityEngine;
using System.Collections;
public class Zombie$$anonymous$$ovement : $$anonymous$$onoBehaviour {
private GameObject playerPos;
public float moveSpeed;
public float AttackAnimDuration;
private bool paused;
IEnumerator delay()
{
paused = true;
yield return new WaitForSeconds(AttackAnimDuration);
paused = false;
}
void Start () {
playerPos = GameObject.FindGameObjectWithTag ("Player"); // replace "Player" with whatever tag your Player has
}
void Update () {
if (paused)
{
return;
}
float move = moveSpeed * Time.deltaTime;
transform.position = Vector3.$$anonymous$$oveTowards (transform.position, playerPos.transform.position, move);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Player")
{
StartCoroutine(delay());
}
}
}
Just let me know if you have any other problems. I like to help.
omg, you are god.
So basicly, all i had to do was to add a transform in playerPos.transfor.position... Why did that make it work?:o
And you also had to get a reference of the player by using GameObejct.FindGameObjectWithTag ("Player") in the Start function.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Spawn random Obstacles along the x-axis 0 Answers
player spawning 0 Answers
Distribute terrain in zones 3 Answers
How to Spawn after checking if the clones are destroyed. 1 Answer