Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Kaleopix · Jan 22, 2017 at 12:18 AM · instantiatespawntagdestroygameobjectmultiple objects

Spawn and destroy gameObject by tag

I'm trying to make a function where a game object is destroyed and its position is used to spawn another game object. For details, my game have robots with multiple states of completion (from state0 to state3). Each state will enter an activator that is used to spawn the next state of completion as well as destroy the previous state(state0 enter activator, state1 is spawned, state0 is destroyed). The activator reads the game object's tag to verify whether it is the right state or not. The activator uses the previous state's position for the spawn position of the next position. The activator also only spawns and destroy when a key is pushed. Here is my code:`using UnityEngine; using System.Collections;

public class StateTransistorActivator : MonoBehaviour {

 public KeyCode key;
 bool active = false;
 public GameObject initialState;
 public GameObject transitionState;
 GameObject robot;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {

     if(Input.GetKeyDown(key) && active)
     {
         Instantiate(transitionState, robot.transform.position, Quaternion.identity);
         Destroy(robot);
     }
 
 }

 void OnTriggerEnter2D(Collider2D col)
 {
     
     active = true;
     
     if (col.gameObject.tag == initialState.gameObject.tag)
     {
         Debug.Log("state0 is true");
         robot = col.gameObject;
     }

 }

 void OnTriggerExit2D(Collider2D col)
 {
     active = false;
 }

The initialState variable is for the previous state game object which is used to compare the game object tag and the transitionState is used spawn the next state's game object.

This code works, but the problem I'm having is that i could not get it to work for multiple game objects of the same state. Basically if I have 2 game objects of the same state (clones of it) the activator only works for one of them, and I needed it to work in case for multiple similar game objects exists.

Here's some picturesalt text

The purple thing is the state0, the rectangles are the activators.

alt text

The first robot transitioned correctly but no matter what I press the second one does not transitioned.

help.png (10.6 kB)
help2.png (28.0 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn a random object on more than one position 1 Answer

Want to instantiate Prefab in its parent. 1 Answer

What is the most efficient way to spawn in a lot of cubes, like 10k? [c#] 2 Answers

How can I make an object clone in the time that I indicate it to appear? 0 Answers

Problem with Instantiated clones 1 Answer


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