I am having a problem that I trying to loop though the level template items and add them to an object for later use but it only saves the one position and never updates when i add it. It will update the position but then it grabs the old position.
Thanks
for (int i = 0; i < asteroid_length; i++) //spawning asteroids
{
//puts the asteroids in the template into the universes so that we can load them later
//universes[x].GetComponent<SystemScript>().asteroids.Add(levels[randomLevel].GetComponent<LevelGenerationScript>().asteroids[i]);
//these are to hold the place we need to add it to grab the asteroid from the objAsteroid so just get transform?
Transform asteroid_transform = levels[randomLevel].GetComponent<LevelGenerationScript>().asteroids[i].transform;
Debug.Log(asteroid_transform.position + " the first");
int randomIndex = 0; //Random.Range(0, objAsteroids.Length);
GameObject new_asteroid = objAsteroids[randomIndex];
Debug.Log(new_asteroid.transform.position+ "");
//new_asteroid = new GameObject(new_asteroid.name);
new_asteroid.transform.position = asteroid_transform.position;
new_asteroid.transform.rotation = asteroid_transform.rotation;
new_asteroid.transform.localScale = asteroid_transform.localScale;
Debug.Log(new_asteroid.transform.position + " the second");
universes[x].GetComponent<SystemScript>().asteroids.Add(new_asteroid);
Debug.Log(new_asteroid.transform.position + " the third");
}
see it changes on the debug but when i used the add to the list it only adds the first position to every single one.
Yes if you look at the picture and code. It gives the right position but then does not put the new one in it puts the old one in… is this a unity thing on how the backend works?
Most likely the code that you wrote just doesn’t actually do what you think it does. Kurt posted a link to instructions for how you can troubleshoot that.
It does do what i think it does… it does not store the new one. Did you look at the code and the image showing the output?
In the image it always takes that first one and never takes the changed position, even though it says it changes it, when i add it to the list. Why is that?
The problem is that you write the position to the console and then you change it. This leads to different results of what it says in the consolve VS the actual position of the asteroids.
Solution: Debug.Log after all transform manipulations (such as someTransform.position = somePosition;) are done.
I have three done, see code and the image attached, in order so I can see that. It always takes the transform of the prefab I am using when i add it to the list, instead of updating it to the new ones.
You’re doing an insane amount of stuff up there, I’m not even going to TRY to understand it.
You only need THREE things:
GameObject prefab = ... however you reach over and get the prefab
Vector3 position = ... however you get position
GameObject newAsteroid = Instantiate<GameObject>( newAsteroid, position, Quaternion.identity);
That’s it. If you need to do more, ONLY do it to newAsteroid
Its a prefab, objAsteroids is a list of prefabs I want to grab to give the look of “randomness” and then put on spawn points I have in the level template. I want to store it in a list (called List asteroids) and I want to instantiate later when needed. I want to just create all the levels at the start and store them in this list Instantiate when needed, update the list, then destroy when not needed.
I mean you can, but you wouldn’t do it via prefabs/game objects. If you want to pre-roll a collection of random layouts, you would need to do so purely via data. So instead of moving around prefabs, you could have a List<Vector3> of positions that you generate. Then, you just need to instantiate a random prefab at each of the given locations at the appropriate times.
You can you just have to do it in a way that the engine supports. Instantiate isn’t just for spawning something that isn’t in the scene. It’s also able to create duplicates of an object in a scene. How you get the object into the scene is up to you. You could manually place it there at design time or instantiate it from a prefab at runtime.
Then whenever you need that template you simply instantiate a copy and enable it.