- Home /
The question is answered, right answer was accepted
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).
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;
}
I can't. Unity complains, Cant implicitly convert type Object to GameObject
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.
Would you review my edited code for me please? I cant get this to work still :/
$$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.
$$anonymous$$eep in $$anonymous$$d that in this case 'Player' is the prefab, not the instance!
Follow this Question
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