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);
}
}
ADNCG
September 30, 2015, 6:33pm
2
try
GameObject temp = (GameObject) Instantiate(randomObject[Random.Range(0, 10)], new Vector3(-686f, 0f, 0f), transform.rotation);
roger0
September 30, 2015, 9:55pm
4
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
Nigey
October 1, 2015, 7:22am
6
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;
Nigey:
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.
Nigey
October 1, 2015, 3:08pm
8
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.
Nigey:
It says here http://www.tasharen.com/forum/index.php?topic=4153.0 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;
}
}