[SOLVED] instantiating and deleting game objects.

Hey there!

I am having trouble creating a script that on user input will instantiate certain objects, and delete the previous array of objects. The delete portion of my code is working but it will not instantiate any objects.
Also I need to be able to pick the location of which they spawn in.
How will that be possible?

public class random : MonoBehaviour {
public GameObject[] tripAssets;
public GameObject[] trip2;
private GameObject gameObjects;
//Usethisfor initialization
void Start () {

}
//Updateiscalledonceper frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
Debug.Log ("hit");
//destroyPrevious();
NextTrip ();
}
}

void destroyPrevious() {
tripAssets = GameObject.FindGameObjectsWithTag ("trip1");
for(var i = 0 ; i < tripAssets.Length ; i ++){
Destroy(tripAssets);
}

}
void NextTrip() {
trip2 = GameObject.FindGameObjectsWithTag ("trip2");
for (var i = 0 ; i < trip2.Length; i++) {
//Instantiate(tripAssets);
Instantiate(trip2, transform.position, Quaternion.identity);
Debug.Log ("finally");
}

}

}

I think you want to review the second or third post in this forum on how to post code using the special formatting available. Your posted images are not a very optimal way to post code since they cannot be copy/pasted.

As for the one script that is visible, I cannot see where you call NextTrip().

ahh ok. everything should be fixed. thanks for letting me know

In your for loop, you are destroying the entire array for every iteration instead of destroying each element of the array.
I suspect that it may be throwing a null reference exception when you call destroyPrevious(). To properly destroy each
element, the loop should look more like this:

for(var i = 0; i < tripAssets.Length; i++) {

     Destroy(tripAssets[i]);
}

Likewise, you may need to modify your instantiation method to iterate through each array element.

I updated the code now, It deletes every element but it is still not instantiating “trip2”

May I see your instantiation code please? And please enclose it within a code formatter.
Do that by clicking the insert button in the text editing tools and chosing code.

sure thing, here.

void NextTrip() {
        trip2 = GameObject.FindGameObjectsWithTag ("trip2");
        for (var i = 0 ; i < trip2.Length; i++) {
            Instantiate(trip2[i], transform.position, Quaternion.identity);
            Debug.Log (trip2);
        }

    }

Does changing that section to

void NextTrip() {
        trip2 = GameObject.FindGameObjectsWithTag ("trip2");
        for (var i = 0 ; i < trip2.Length; i++) {
            Instantiate(trip2[i], transform.position, Quaternion.identity) as GameObject;
            Debug.Log (trip2);
        }

    }

fix anything?

throws an error saying “Only assignment,call, increment, await and new object expressions used as a statement”

Your code block appears to be correctly formatted. One thing that you possibly need to check is:

  1. When trip2 is assigned the result of the FindGameObjectsWithTag, does it actually find them?
    If it does not, you may want to be sure that you included the correct tags on those objects.
    You can check this by doing a debug log for each loop iteration:Debug.Log(trip2[i].name);

Note that you are trying to debug.log an array and not a string in your loop.
A better option would be the afore mentioned. Chances are that just trying
to do that could prevent your code from executing properly and fully.

I have a question for you:

I am curious why you are searching for existing scene objects and then re-instantiating or cloning them?

Derp, my bad, of course it did. Didn’t assign it to anything.

void NextTrip() {
        trip2 = GameObject.FindGameObjectsWithTag ("trip2");

        foreach (GameObject t in trip2) {
            Instantiate(t, transform.position, Quaternion.identity);
        }
    }

If this doesn’t work, something’s funky.

You know, though, that this is instantiating an already existing object in the same place as whatever is calling this script, right? Your instantiated game Objects are going to appear directly ontop of the spawner. If you want them to retain their original positions, you’re gonna have to get the position and rotation of each t in trip2.

Ps- I’d recommend using a prefab instead of preexisting gameObjects.

If you wanted your new objects to be where the originals are, do this

void NextTrip() {
        trip2 = GameObject.FindGameObjectsWithTag ("trip2");

        foreach (GameObject t in trip2) {
            Instantiate(t, t.transform.position, t.transform.rotation);
        }
    }

on user input, I want to change a set of assets in a scene.
I want to replace the previous ones with new ones in the exact same transform.
like lets say I have a chair in a scene, I am trying to on user input, change the chair they see.
But i also need the ability to go back to the previous asset on input

each set of objects in the array will have a different transform though, which is the part that is confusing me

Are you using instances to replace the current scene objects or prefabs? You should use prefabs if possible.
If I understand you correctly, you want your user to be able to change out a set of game objects when they
press the space bar. Are you really wanting to grab those new objects from the scene or could you use prefabs instead?
If you could (as you should) use prefabs, you can simply make a reference to them in your script as public objects
and assign them in the inspector. That would save you from having to do a FindGameObjects call which is the
recommended approach. If you do want to grab them from the scene, disregard this but you should consider
using prefabs for most of your scene objects.

I will switch to prefabs.
Yes I planned on using prefabs once this is implemented.
Would that allow me to put different prefabs in a scene at once in different locations with a single for loop though?

You should be able to instantiate any number of prefabs within the loop body. Simply call instantiate on each in a new statement.

ahh. I can see how that would work but it is still just deleting the objects and no new ones are being made.
definitely an error on my end somewhere :frowning:

ahh ok. Because the objects I want to instantiate are not in the hierarchy window.
I dont think there would be a parent object?