instantiate sprite prefab at a certain position??? HELP

Hello!

I want my sprite prefab to spawn at (-686,0) but it always spawn at (0,0,0).

using UnityEngine;
using System.Collections;

public class SpawnRandom : MonoBehaviour {

public GameObject [ ] randomObject;

void Start(){
StartCoroutine (WaitAndSpawn (2.0F));

}

// Update is called once per frame
void Update () {

}

IEnumerator WaitAndSpawn(float spawn){
yield return new WaitForSeconds (spawn);
Instantiate (randomObject [Random.Range (0, 10)], new Vector3(-686, 0, 0), transform.rotation);
}

}

try

GameObject temp = (GameObject) Instantiate(randomObject[Random.Range(0, 10)], new Vector3(-686f, 0f, 0f), transform.rotation);

Didn´t work :confused:

I tried the script in my project and it seems to be working there.

Have you tried spawning a regular object like a box?

it works with a normal sprite but not with a NGUI sprite :confused:

Create a reference to the object and try doing a few ‘things’ to it, to see if something else in the world is affecting your sprite.

GameObject exampleObj = Instantiate(randomObject[Random.Range(0, 10)], new Vector3(-686, 0, 0), transform.rotation) as GameObject;
            exampleObj.transform.position = somePosition;

didn´t work either… Maybe it´s something to do with NGUI.

It says here Setting transform.position to set transform.localPosition, instead of transform.position. I don’t use NGUI, but I’d imagine it’s code would control something like it’s overall position with a hefty chunk of code lol.

yy, but i solved it now! in strange way ahha. looked at a utube video and took some code and then it just worked haha

1 Like

using UnityEngine;
using System.Collections;

public class SpawnRandom : MonoBehaviour {

public GameObject [ ] randomObject;
public GameObject exampleobj;
public Transform gen;

void Start(){
StartCoroutine (WaitAndSpawn (2.0F));

}

// Update is called once per frame
void Update () {

}

IEnumerator WaitAndSpawn(float spawn){
yield return new WaitForSeconds (spawn);
exampleobj = Instantiate(randomObject[Random.Range(0,10)], new Vector3(-1.9f,0f,0f), transform.rotation) as GameObject;

Vector3 size = exampleobj.transform.localScale;

exampleobj.transform.parent = gen;
exampleobj.transform.localScale = size;

}

}