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 /
avatar image
0
Question by jwalkaz33 · Apr 16, 2020 at 04:28 AM · instantiateprefabpositioning

Prefab not instatiating at correct position

So Im trying to instantiate a unit with a z offset of 5 from the factory instantiating it but the new object just goes to (0,0,5) instead of the factorys transform.position + offset.

 public class SpawnInfantryUnits : MonoBehaviour
 {
     public GameObject lesserDeathUnit;
     public GameObject lesserWarUnit;
     public static GameObject lesserDeath;
     public static GameObject lesserWar;
     Vector3 spawnSpot;
     public Vector3 posOffset;
 
 
     private void Start()
     {
         spawnSpot = transform.position;
         print(spawnSpot);
         spawnSpot += posOffset;
         print(spawnSpot);
     }
 
     private void OnMouseDown()
     {
         UIManager.InfantryUnits.SetActive(true);
         UIManager.Research.SetActive(false);
         UIManager.buildMain.SetActive(false);
         UpgradeSupply.upgradeSupply.SetActive(false);
         UIManager.Min.SetActive(true);
         // UIManager.unitMain.SetActive(false);
     }
 
     public void SpawnLesserDeath()
     {
         if (Stats.population < Stats.maxPopulation)
         {
             print("pop check pass");
 
             print(spawnSpot);
 
             if (Stats.money >= 125)
             {
                 lesserDeath = (GameObject)Instantiate(lesserDeathUnit, spawnSpot, Quaternion.identity);
                 //lesserDeath.transform.parent = gameObject.transform;
                 Stats.units.Add(lesserDeath);
                 Stats.money -= 125;
                 print("Death Spawn");
             }
             else
             {
                 BuildingSelect.SupplySelected = false;
                 print("Too poor");
             }
         }
     }
 
     public void SpawnLesserWar()
     {
         if (Stats.population < Stats.maxPopulation)
         {
             print("pop check pass");
 
             if (Stats.money >= 125)
             {
 
                 lesserWar = (GameObject)Instantiate(lesserWarUnit, transform.position + posOffset, Quaternion.identity);
                 //lesserWar.transform.parent = gameObject.transform;
                 Stats.units.Add(lesserDeath);
                 Stats.money -= 125;
                 print("War Spawn");
             }
             else
             {
                 BuildingSelect.SupplySelected = false;
                 print("Too poor");
             }
         }
     }
 }

Comment
Add comment · Show 6
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 wertyq · Apr 16, 2020 at 05:01 AM 0
Share

spawnSpot prints (0, 0, 5) or the desired position?

avatar image jwalkaz33 wertyq · Apr 16, 2020 at 05:05 AM 0
Share

In start spawnSpot prints the desired position then prints 0, 0, 5 in SpawnLesserDeath(). it changes and I'm not sure why?

Edit: and since it changes it doesn't instantiate at the desired position

avatar image highpockets jwalkaz33 · Apr 16, 2020 at 07:34 AM 0
Share

Is the factory start position (0,0,0)?? If that is the case, you will always get (0,0,5). You would need to update the position inside SpawnLesserDeath() like you are doing in the instantiate method in SpawnLesserWar()

Show more comments

1 Reply

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

Answer by jwalkaz33 · Apr 22, 2020 at 05:53 AM

when it was instantiating it was taking the prefab of the spawner object instead of the one that was in the scene so I had to get a reference to the one in the scene then use its transform

  public void SpawnLesserDeath()
     {
         if (selectedBuilding.currentBuilding.Count > 0)
         {
             if (Stats.population < Stats.maxPopulation)
             {
                 // print("pop check pass");
 
                 if (Stats.money >= 125)
                 {
                     lesserDeath = (GameObject)Instantiate(lesserDeathUnit, selectedBuilding.currentBuilding[0].transform.parent.parent.position + posOffset, Quaternion.identity);
                     Stats.units.Add(lesserDeath);
                     Stats.money -= 125;
                     //print("Death Spawn");
                 }
                 else
                 {
                     BuildingSelect.SupplySelected = false;
                     //print("Too poor");
                 }
             }
         }
     }

getting the object in the scene

  public static List<GameObject> currentBuilding = new List<GameObject>();
 
     private void Update()
     {
         if (Input.GetMouseButton(1))
         {
             currentBuilding.Clear();
         }
     }
 
     private void OnMouseDown()
     {
         if (currentBuilding.Count > 0)
         {
             currentBuilding.Clear();
             currentBuilding.Add(gameObject);
             print(currentBuilding.ToString());
             print("game object removed and new added" + gameObject.name);
         }
         else
         {
             currentBuilding.Add(gameObject);
             print(currentBuilding.ToString());
             print("game object added" + gameObject.name);
         }
     }


Comment
Add comment · 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

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

178 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I position an instantiated row of prefabs? 1 Answer

Wrong particle and prefab spawn position 1 Answer

Stop Children of Instantiated Parent From Offsetting After Changing Position 0 Answers

Prefabs dont instantiate directly on point 0 Answers

Referencing gameObject from script after Instantiate 0 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