Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jul 06, 2014 at 10:34 PM by QuestionAsker for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by QuestionAsker · Sep 22, 2013 at 11:09 PM · c#instantiatetransformparent

Getting transform from an instantiated clone to parent it?

void SpawnPlayer (Transform ship) {
Vector3 centPos = new Vector3(0,0,0);

         Object playerP = Network.Instantiate(Player, centPos, centPos, mainCamGroup);
         
         ship.parent = playerP.transform;
     }

Ship is another instantiated object.

I need to obtain the transform from an initiated clone, however the type of instantiate is 'Object' which has no Transform type.

How do I do what i'm trying to achieve?

Thanks in advance, and please ask if you have questions! Ill be monitoring constantly for now.

EDIT---------- using UnityEngine; using System.Collections;

 public class GameManager : MonoBehaviour {
     
     /// <summary>
     /// This script is attached to the 'Game Manager' and 
     /// allows player to spawn, after selecting the pre game options
     /// </summary>
 
     //Variables-----------------------------------------------------------------
     
     private bool justConnectedToServer = false;
     
     //Used to determine which civ player is on.
     
     public bool american = false;
     
     public bool british = false;
     
     private int mainCamGroup = 0;
     
     //Used to defined join civ window
     
     private Rect joinCivRect;
     
     private string joinCivWindowTitle = "Select Your Mother Civilation";
     
     private int joinCivWindowWidth = 330;
     
     private int joinCivWindowHeight = 100;
     
     private int joinCivLeftIndent;
     
     private int joinCivTopIndent;
     
     private int buttonHeight = 40;
     
     public GameObject Player;
     
     public GameObject amCivMothShip;
     
     public GameObject britCivMothShip;
     
     private GameObject[] eastSpawnPoints;
     
     private int amCivGroup = 1;
     
     private int britCivGroup = 2;
     
     
     //End of Variables----------------------------------------------------------
     void OnConnectedToServer ()
     {
         justConnectedToServer = true;    
     }
     
     
     void PickMotherCivWindow (int windowID)
     {
         //If the player clicks join American button, they're assigned to American and are spawned.
         if(GUILayout.Button("American", GUILayout.Height(buttonHeight)))
         {
             american = true;
             
             justConnectedToServer = false;
             
             SpawnAmericanMothShip();
             
         }
         
         //If the player clicks join british button, they're assigned to british and are spawned.
         if(GUILayout.Button("British", GUILayout.Height(buttonHeight)))
         {
             british = true;
             
             justConnectedToServer = false;
             
             SpawnBritishMothShip ();
         }
     }
     
     
     void OnGUI () 
     {
         if(justConnectedToServer == true)
         {
             joinCivLeftIndent = Screen.width / 2 - joinCivWindowWidth / 2;
             
             joinCivTopIndent = Screen.height / 2 - joinCivWindowHeight / 2;
             
             joinCivRect  = new Rect(joinCivLeftIndent, joinCivTopIndent, 
                                     joinCivWindowWidth, joinCivWindowHeight);
             
             joinCivRect = GUILayout.Window(0, joinCivRect, PickMotherCivWindow, joinCivWindowTitle);
         }
         
     }
     
     void SpawnAmericanMothShip () 
     {    
         //Find all spawn points, and establish a reference in an array.    
         
         eastSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnEast");
         
         //Below will randomly select a spawn point
         
         GameObject eastSpawnPoint = eastSpawnPoints[Random.Range(0, eastSpawnPoints.Length)];
         
         //Instantiate player at random spawn
         
         GameObject ship = (GameObject)Network.Instantiate(amCivMothShip, eastSpawnPoint.transform.position,
             eastSpawnPoint.transform.rotation, amCivGroup);
         
         SpawnPlayer((GameObject)ship);
     }
     
     
     void SpawnBritishMothShip ()
     {
         //Find allspawn points, and establish a reference in an array.
         eastSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnEast");
      
         //Below will randomly select a spawn point
         GameObject eastSpawnPoint = eastSpawnPoints[Random.Range(0, eastSpawnPoints.Length)];
      
         //Instantiate ship at random spawn
         GameObject ship = (GameObject)Network.Instantiate(britCivMothShip, eastSpawnPoint.transform.position,
         eastSpawnPoint.transform.rotation, britCivGroup);
      
         SpawnPlayer((GameObject)ship);
     }
      
     void SpawnPlayer (GameObject ship)
     {
         Vector3 centPos = new Vector3(0,0,0);
         
         GameObject playerP = (GameObject)Network.Instantiate(Player, centPos, Quaternion.Euler(0,0,0), mainCamGroup);
         
         ship.transform.parent = playerP.transform;
     }
 }



Im sorry for the long script, but since i'm probably making a stupid mistake, id rather not edit out the useless stuff.

As a side note, i double checked the assignments to the GameObjects AmCivMothShip, BritCivMothShip, and player.

As a side side note, 'civ' is short for civilization, 'moth' is short for mother. Am and Brit are american and british respectively(obviously).

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

  • Sort: 
avatar image
1
Best Answer

Answer by clunk47 · Sep 22, 2013 at 11:23 PM

Use GameObject instead of Object to instantiate.

 public GameObject Player;
 public GameObject Ship;
 
 void SpawnShip ()
 {
     //Find allspawn points, and establish a reference in an array.
     eastSpawnPoints = GameObject.FindGameObjectsWithTag("SpawnEast");
     
     //Below will randomly select a spawn point
     GameObject eastSpawnPoint = eastSpawnPoints[Random.Range(0, eastSpawnPoints.Length)];
     
     //Instantiate ship at random spawn
     GameObject ship = (GameObject)Network.Instantiate(britCivMothShip, eastSpawnPoint.transform.position,
     eastSpawnPoint.transform.rotation, britCivGroup);
     
     SpawnPlayer((GameObject)ship);
 }
     
 void SpawnPlayer (GameObject ship)
 {
     Vector3 centPos = new Vector3(0,0,0);
     GameObject playerP = (GameObject)Network.Instantiate(Player, centPos, Quaternion.Euler(0,0,0), mainCamGroup);
     ship.transform.parent = playerP.transform;
 }
Comment
Add comment · Show 14 · 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 QuestionAsker · Sep 22, 2013 at 11:26 PM 0
Share

I can't. Unity complains, Cant implicitly convert type Object to GameObject

avatar image clunk47 · Sep 22, 2013 at 11:36 PM 1
Share

You need to have your 'Player' as a GameObject as well, then cast the instantiation to GameObject:

 public GameObject Player;
 
 GameObject playerP = (GameObject)Network.Instantiate(Player, centPos, centPos, mainCamGroup);

Pretty much, don't use Object anywhere in your code. Use GameObject.

avatar image QuestionAsker · Sep 23, 2013 at 12:02 AM 0
Share

Would you review my edited code for me please? I cant get this to work still :/

avatar image clunk47 · Sep 23, 2013 at 12:11 AM 2
Share

$$anonymous$$ake clear, what are you trying to parent to what? If you are trying to parent the ship to the new playerP object, use ship.transform.parent = playerP.transform, not Player.transform.

avatar image syclamoth · Sep 23, 2013 at 12:14 AM 1
Share

$$anonymous$$eep in $$anonymous$$d that in this case 'Player' is the prefab, not the instance!

Show more comments

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Setting parent of instantiated sprite 2 Answers

Instantiating as a child error 1 Answer

Instantiated GameObject gets spawned as a child 2 Answers

Instantiate a Prefab as child 0 Answers

Simple instantiate a variable 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