Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

instantiate sprite prefab at a certain position??? HELP

Discussion in 'Scripting' started by SatsArtsStudio, Sep 30, 2015.

  1. SatsArtsStudio

    SatsArtsStudio

    Joined:
    Apr 5, 2014
    Posts:
    10
    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);
    }

    }
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    try

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

    SatsArtsStudio

    Joined:
    Apr 5, 2014
    Posts:
    10
    Didn´t work :/
     
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I tried the script in my project and it seems to be working there.

    Have you tried spawning a regular object like a box?
     
  5. SatsArtsStudio

    SatsArtsStudio

    Joined:
    Apr 5, 2014
    Posts:
    10
    it works with a normal sprite but not with a NGUI sprite :/
     
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    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.

    Code (CSharp):
    1. GameObject exampleObj = Instantiate(randomObject[Random.Range(0, 10)], new Vector3(-686, 0, 0), transform.rotation) as GameObject;
    2.             exampleObj.transform.position = somePosition;
     
  7. SatsArtsStudio

    SatsArtsStudio

    Joined:
    Apr 5, 2014
    Posts:
    10
    didn´t work either... Maybe it´s something to do with NGUI.
     
  8. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    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.
     
  9. SatsArtsStudio

    SatsArtsStudio

    Joined:
    Apr 5, 2014
    Posts:
    10
    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
     
    Nigey likes this.
  10. SatsArtsStudio

    SatsArtsStudio

    Joined:
    Apr 5, 2014
    Posts:
    10
    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;

    }

    }