- Home /
Instantiating a prefab spawns it in the wrong location.
I have a system where you go up to a sign and click on it to bring up a menu to select a building to spawn there. I have an empty gameobject holding my PurchaseBuilding script and on that script it deals with getting the name and position to spawn the cube prefab. On the signs prefab, I have a script that gets the position of the sign and returns it and is called on the PurchaseBuilding script. However, when I have multiple sign prefabs in the scene, it defaults to spawning on my first sign.
Here's my code for the PurchaseBuilding script: public class PurchaseBuilding : MonoSingleton { public GameObject saleSign;
string selectedBuilding;
Vector3 pos;
public void SelectBuilding()
{
var selectedButton = EventSystem.current.currentSelectedGameObject.name;
if (selectedButton == "Button")
{
selectedBuilding = "BlueBuilding";
}
else if(selectedButton == "Button (1)")
{
selectedBuilding = "RedBuilding";
}
else if(selectedButton == "Button (2)")
{
selectedBuilding = "YellowBuilding";
}
pos = GetPositionToSpawnBuilding.Instance.GetPosition();
GameObject myBuilding = Instantiate(Resources.Load(selectedBuilding), (pos + new Vector3(0, 10, 0)), Quaternion.identity) as GameObject;
saleSign.SetActive(false);
Cursor.lockState = CursorLockMode.Locked;
GameObject.Find("Main Camera").GetComponent<MouseLook>().enabled = true;
PlayerMovement.playerHasInput = true;
PlayerMovement.inMenu = false;
}
Here's the code that gets the sign position:
public class GetPositionToSpawnBuilding : MonoSingleton<GetPositionToSpawnBuilding>
{
[SerializeField] Vector3 pos;
private void Start()
{
pos = gameObject.transform.position;
}
public Vector3 GetPosition()
{
return pos;
}
}
Answer by MichaI · Jun 26, 2020 at 06:40 PM
Hi,
Your GetPositionToSpawnBuilding is a singleton, so from its definition you are using only one istance of it. To get position of specific sign you have to change GetPositionToSpawnBuilding to normal MonoBehaviour and somehow pass it to PurchaseBuilding from the proper sign object. For example:
public void SelectBuilding(GetPositionToSpawnBuilding positionScript)
{
var selectedButton = EventSystem.current.currentSelectedGameObject.name;
if (selectedButton == "Button")
{
selectedBuilding = "BlueBuilding";
}
else if(selectedButton == "Button (1)")
{
selectedBuilding = "RedBuilding";
}
else if(selectedButton == "Button (2)")
{
selectedBuilding = "YellowBuilding";
}
pos = positionScript.GetPosition();
GameObject myBuilding = Instantiate(Resources.Load(selectedBuilding), (pos + new Vector3(0, 10, 0)), Quaternion.identity) as GameObject;
saleSign.SetActive(false);
Cursor.lockState = CursorLockMode.Locked;
GameObject.Find("Main Camera").GetComponent<MouseLook>().enabled = true;
PlayerMovement.playerHasInput = true;
PlayerMovement.inMenu = false;
}
or get this position earlier and pass just position:
public void SelectBuilding(Vector3 pos)
{
var selectedButton = EventSystem.current.currentSelectedGameObject.name;
if (selectedButton == "Button")
{
selectedBuilding = "BlueBuilding";
}
else if(selectedButton == "Button (1)")
{
selectedBuilding = "RedBuilding";
}
else if(selectedButton == "Button (2)")
{
selectedBuilding = "YellowBuilding";
}
GameObject myBuilding = Instantiate(Resources.Load(selectedBuilding), (pos + new Vector3(0, 10, 0)), Quaternion.identity) as GameObject;
saleSign.SetActive(false);
Cursor.lockState = CursorLockMode.Locked;
GameObject.Find("Main Camera").GetComponent<MouseLook>().enabled = true;
PlayerMovement.playerHasInput = true;
PlayerMovement.inMenu = false;
}
Your answer
Follow this Question
Related Questions
Creating a GameObject variable without instantiating it? 1 Answer
Instantiating a prefab and values 1 Answer
The object is not output. 0 Answers
Multiple Cars not working 1 Answer
Retain references to child gameobjects while duplicating objects 0 Answers