• 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 /
This question was closed Apr 26, 2017 at 05:50 AM by Jorghe for the following reason:

The question is solved

avatar image
0
Question by Jorghe · Apr 22, 2017 at 12:47 AM · instantiategetcomponent

Assign a Side to a spawned Unit in a two sided Towers game

I am making a Tower defense kind of game, I'm trying to spawn units from one side or another using different keys, the problem is that I must assign a "Side" and a "Target" side is the same as the playerNumber and Target is assigned with this condition:

     if (playerNumber == 1)
     {
         target = 2;
         spawnX = transform.position.x + 5;
         spawnY = transform.position.y + 10;
         direction = Vector3.right * speed;
     }
     else if (playerNumber == 2)
     {
         target = 1;
         spawnX = transform.position.x - 5;
         spawnY = transform.position.y - 10;
         direction = Vector3.left * speed;

     }

And the function Spawn is called with a trigger button in Update, spawning works correck, the problem is that when Assigning a Side and a Target, some of them are correct and sometimes they are not, it is usually the first, if I spawn 5 units, sometimes the first is with the wrong side.

It gets weird because it sometimes spawns with the correct side and sometimes not, so it makes it hard to debug.

private void Spawn() {

     spawnCount++;

     Rigidbody2D clone;
     clone = Instantiate(unit, new Vector3(spawnX, spawnY, spawnZ), transform.rotation) as Rigidbody2D;
     clone.velocity = transform.TransformDirection(direction);

     // Assign a side, used to be spawned in one side and attack its target 
    
     unit.GetComponent<UnitAttack>().unitSide = playerNumber;

     unit.GetComponent<UnitAttack>().unitTarget = target;
     

 }
Comment
Add comment · Show 1
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 Jorghe · Apr 23, 2017 at 09:09 AM 0
Share

I've noticed that this only happens when I spawn from one side, its done correctly, but if i choose the other side to spawn that unit becomes the enemy and since i'm instantiating near the player, its damaging itself. When I spawn at the same time, from both sides, the second spawn gets the values of the first, and therefore damages itself.

0 Replies

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Get unity to recognize prefab in C# 2 Answers

How do I acces a specific GameObject within the terrain???? 0 Answers

Assigning id to instantiated objects 1 Answer

Instantiate ID 2 Answers

Photon instantiate (Assign Instantiated . get component 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