1

This may seem like a stupid question but I'm stuck with it. I have GameObjects in a list (List<GameObject>) and I want to add them on the scene runtime, prefarbly on predefined places (like placeholders or something). What would be a good way to do it? I've been searching the net but can't really find anything that would solve this. This is my code so far:

public static List<GameObject> imglist = new List<GameObject>();
private Vector3 newposition;
public static GameObject firstGO;
public GameObject frame1;//added line

void Start (){
newposition = transform.position;
firstGO = GameObject.Find ("pic1");
frame1 = GameObject.Find ("Placeholder1");//added line

//this happens when a button is pressed
imglist.Add(firstGO);
foreach(GameObject gos in imglist ){
            if(gos != null){
                print("List: " + gos.name);
                try{
                    //Vector3 temp = new Vector3 (0f, 0f, -5f);
                    Vector3 temp = new Vector3( frame1.transform.position.x, frame1.transform.position.y, -1f);//added line
                    newposition = temp;
                    gos.transform.position += newposition;
                    print ("position: " + gos.transform.position);
                }catch(System.NullReferenceException e){}
            }
        }
}

How can I place the pics (5) on the predefined spots?

//----------------

EDIT: Now I can place 1 image to a placeholder (transparent png). For some reason z-value goes all over the place so it needs to be forced to -1f but that's OK. I add the images to the list from other scenes and there can be 1-5 of them. Do I need to put the placeholders in another list or array? I'm a bit lost here.

1

3 Answers 3

1

If you've already created 5 new objects you can just do like they do here: http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke under the InvokeScript

foreach(GameObject gos in imglist)
{
    Instantiate(gos, new Vector3(0, 2, 0), Quaternion.identity);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I don't really understand what you're trying to do, but if I'm correct and you have a list of objects, and you know where you want to move them at runtime, just make two lists, one containing the objects and one containing transforms of empty game-objects in the scene placed at those predefined positions, and match them at runtime.

Populate both lists from the inspector.

public List<GameObject> imglist = new List<GameObject>();
public List<Transform> imgPositions = new List<Transform>();


void Start()
{
    for(var i = 0 i < imglist.Count; ++i)
    {
        imglist[i].transform.position = imgPositions[i].position
    }
}

1 Comment

I came to an almost identical solution myself, I made a Vector3 list (public static List<Vector3> coordinates = new List<Vector3>(); ) of the placeholders and changed the foreach into a for so I can read both lists with the same iterator.
0

The general best way is to create prefabs for your objects, passing them as a parameter and instantiate when needed (Start in your case). That's the common case, but maybe yours is slightly different.

This is an example of passing a prefabs array and to instantiate one object for each one in the array:

public GameObject prefabs[];
List<GameObject> objects = new List<GameObject>();

void Start() {
    for(GameObject prefab in prefabs) {
        GameObject go = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; // Replace Vector3.zero by actual position

        objects.Add(go); // Store objects to access them later: total enemies count, restart game, etc.
    }
}

In case you need several instances for the same prefab (multiple enemies or items, for instance) just adapt code above.

2 Comments

Using prefabs this way is quite an alien thing to me yet. Thanks for the code, I will test and practice with it :)
That's pretty basic. You should try the tutorials. It will make your life easier. Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.