• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tageos · Jun 26, 2015 at 03:28 PM · c#spawnspawning

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tageos · Jun 26, 2015 at 09:35 PM 0
Share

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.

avatar image Wolfdog · Jun 26, 2015 at 10:28 PM 0
Share

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.

avatar image Tageos · Jun 27, 2015 at 08:44 AM 0
Share

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

avatar image Wolfdog · Jun 27, 2015 at 08:53 AM 0
Share

And you also had to get a reference of the player by using GameObejct.FindGameObjectWithTag ("Player") in the Start function.

avatar image Tageos · Jun 27, 2015 at 09:46 AM 0
Share

yes ofcourse, thank you :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies spawn on top of each other(C#)(Unity) 1 Answer

AI spawning areas. What are the ways to make them? 1 Answer

Instantiate is throwing my throwing my objects? 1 Answer

Enemy spawn, round based 1 Answer

How to make objects appear with mouse click in a row? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges