- Home /
[Not Solved]Instintiated gameobject(sprite) wont load on scene
Hello,
In my code i am instantiaing a gameobject with a sprite multiple times. But only the original gameobject will load the sprite at the correct place. but the others ones wont.
(Im still a newbie so code wont look to nice)
edit: So i made a few changes, but i still have the same problem even though the objects arent in the same place anymore.
Instantiate code:
void Start ()
{
PlaceBlocks();
}
void PlaceBlocks()
{
for( int i = 0 ; i <= 6 ; i++)
{
GameObject _go = (GameObject)Instantiate( _letterBlock ) as GameObject;
_go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3((_blockPosX[0]),(_blockPosY[i]),-2));
this._letterBlockList.Add(_go);
}
for( int i = 0 ; i <= 5 ; i++)
{
GameObject _go = (GameObject)Instantiate( _letterBlock ) as GameObject;
_go.transform.position = Camera.main.ScreenToWorldPoint(new Vector3((_blockPosX[1]),(_blockPosY[i]),-2));
this._letterBlockList.Add(_go);
}
}
}
Instantiated object code:
void Start ()
{
CreateButtons();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.A)){
CreateButtons();
print ("aaaargh");
}
}
void CreateButtons()
{
string _textPath = "Assets/Texture/button 3.png";
Texture2D _buttonText = (Texture2D)Resources.LoadAssetAtPath(_textPath, typeof(Texture2D));
SpriteRenderer renderer = gameObject.GetComponent<SpriteRenderer>();
Sprite sprite = new Sprite();
sprite = Sprite.Create( _buttonText, new Rect(0,0,400,400), new Vector2 (0.5f,0.5f),250.0f);
renderer.sprite = sprite;
renderer.material = _dfltMaterial;
}
void OnMouseDrag()
{
_moveObject.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
_moveObject.y = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
_moveObject.z = -2;
transform.position = _moveObject;
}
}
Answer by jenci1990 · Dec 02, 2014 at 06:24 PM
_go.transform.position = Camera.main.ScreenToWorldPoint(_position);
the "_position" variable never change, so all gameObject are at the same position.
for( int i = 0 ; i <= 6 ; i++) {
GameObject _go = (GameObject)Instantiate( _letterBlock ) as GameObject;
_go.transform.position = Camera.main.ScreenToWorldPoint(_position);
this._letterBlockList.Add(_go);
//Change the postition
_position.x += 1f;
}
$$anonymous$$ade changes but still same problem occurs, and i checked that they arent in the same places anymore.
Your answer
Follow this Question
Related Questions
Instantiate Prefab 1 Answer
Problems with ScreenToWorldPoint in 2D game 1 Answer
Camera ScreenToWorldPoint Instatiating 1 Answer
Instantiated Sprite is not rendering. 1 Answer
Rotating a sprite toward the mouse position on instantiation. 0 Answers