- Home /
Question by
TwoHeadedFetus · Mar 11, 2021 at 02:00 AM ·
c#instantiatetransform.positionobject referenceoop
GameObject changing position when changing reference
I'm trying to make a script where I can spawn copies of an object on click, and have them spawn at my mouse position. I have one GameObject thats constantly following the mouse, and on click, the GameObject is assigned a new value, and I expect that the "old" gameObject to which I don't have a reference anymore would simply fall from the place of the mouse then. This is my script:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
spawn = Instantiate(prefab, Vector3.zero, new Quaternion());
}
spawn.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 9));
}
What happens is the old GameObject gets teleported to a specific part of the world, everytime I click. The transform position is no longer the one at the mouse. Why does this not work? I guess I don't understand these programming concepts yet
Comment