Instantiate can't get clone to show up at vector3 position

I need to make a clone and have it show up at the vector3 position. But every time i do it shows up on the object it is cloning. What am I doing wrong?

#pragma strict
var projectName 			: String 	= "Project Name ";
var projectSelecterXPos 	: float 	= 0.5;
var projectSelecterYPos 	: float		= 0.5;
var projectManagerArray			 		= Array();
var projectSelecterButton 	: Transform;
var x = 0;
function Start () 
{
	guiText.transform.position = Vector3(projectSelecterXPos,projectSelecterYPos,0);
	guiText.text = projectName;
}

function Update () 
{
	NewProject();
}

function NewProject()
{
	if(x<1)
	{ 
		x++;
		Instantiate(projectSelecterButton,Vector3(projectSelecterXPos, projectSelecterYPos - .25, 0), Quaternion.identity);
	}
}

2 Answers

2

there is nothing wrong with the code

  Instantiate(projectSelecterButton,Vector3(projectSelecterXPos, projectSelecterYPos - .25, 0), Quaternion.identity);

this part : Vector3(projectSelecterXPos, projectSelecterYPos - .25, 0)

is the position where the object is created, period.

assuming you have not changed the values of projectSelecterXPos or projectSelecteryPos, the object is instantiated at Vector3(.5,.25,0), if you pause and select the object you will see this.

This is very small scale, practically (0,0,0), which is the center of the world

most likely the object you are cloning is at this position by coincidence, try moving it somewhere else, or select the object which has this script on it, and change the vars to something bigger, like 10 or 100…

EDIT:

on closer inspection, you have further issues… the use of these positions in your GUI CODE is using them as SCREEN COORDINATES, that is in terms of the screen coordinate system…

Instantiate uses a WORLD POSITION, which has nothing to do with the orientation of your camera (screen coordinates)

I can only guess that the object being instantiated is a GUIElement?

in which case you are putting it at the same position as the object which contains this script, note:

function Start ()
{
guiText.transform.position = Vector3(projectSelecterXPos,projectSelecterYPos,0);
//same as
transform.position = Vector3(projectSelecterXPos,projectSelecterYPos,0);
// in other words, this object is in the same position 
}

RE-EDIT:

it seems the object you are cloning also contains this script as well, I presume… note that EVERY TIME an object with this script is created, The Start() function on that script will override any previous position, making it APPEAR to instantiate in the same place… actually, it instantiated where you wanted after all, but then instantly reset to the same place!

Yes the object being Instantiated is a GUIElement. But even if there working off a different coordinates the transform for the cloned object should be different right? But I keep getting them both the same place.

–

press the PAUSE BUTTON select the object with THIS SCRIPT, look in the INSPECTOR, what is the position? select the CLONE, what is the position?

–

I tired adding a cube and attaching the same code to it and changed the .25 to a 5 and i still have the same problem. Both cubes spawn on top of each other rather than one below the other.

–

wait a tick! the object you are cloning, does IT have THIS SCRIPT? if so, it will simply RESET ITS POSITION via ITS OWN START FUNCTION once it is created! just get rid of the line in Start() transform.position = Vector3(projectSelecterXPos,projectSelecterYPos,0);

–

Oh duh XD lol thanks man works perfect now. Have to love the simple mistakes lol.

–

i know its really small but on the game screen its actually a significant jump. But how i know for sure is if you check the transform of the clone it is the same.

.25 is very small in WORLD COORDINATES (when I first read your post, I didn't fully realize it was GUI coordinates) in GUIElement coordinates, though, this represents 1/4 of the sreen size... NOT SO SMALL in this case after all.. ;)

–