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().
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.
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.
Your code block appears to be correctly formatted. One thing that you possibly need to check is:
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.
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.
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
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?