- Home /
Change gameObject within a Transfom
Hi guys, sorry if this is hard to explain.
I have the following code
var checkpoint : Transform;
var checkpoint2 : GameObject;
function Awake () {
transform.position = checkpoint.position;
transform.rotation = checkpoint.rotation;
}
checkpoint is a spawn location, within my game. i have to manually add the prefab/empty object as the spawn location (click and drag into the variable box).
I would like to automatically change that variable, with a different prefab/empty object. This would allow a new spawn location, where ever the new/second prefab is located.
Problem is, you cant Mix a gameobject with a transform. How can i overcome this?
Surely if i can manually add a prefab into the variable, i should be able to change that variable with another prefab, automatically.
Thankyou
Edit: I would like to make "checkpoint2" the new variable within "Checkpoint"
Do you want to assign a GameObject to checkpoint2 in the Inspector in the Editor, or dynamically at runtime during your game?
Also note: checkpoint.gameObject is the associated GO, checkpoint2.transform is the associateed Transform.
Dynamically. So you spawn at checkpoint - Spawn 1 Load a new scene + change checkpoint to Spawn 2 Return to previous scene and checkpoint now = spawn 2, where you spawn into.
I think T27$$anonymous$$ might of solved it for me. thanks anyways :)
Answer by T27M · Feb 04, 2013 at 02:36 PM
You can get the transform of the gameobject with
transform.position = checkpoint2.transform.position;
Ah! I think this is what im missing. I came really close to typing that. but i think i did checkpoint2.position.transform;
It worked. Thankyou! (Now i just gotta figure out if i can use it the way i want to, i think i should be able to, thanks again!)
How many checkpoints are you planning on having? If you want to store many I would store all the potential checkpoints in a list or array and set them based on the `Application.loadedLevelName("levelName");
If you are only going to have one checkpoint per level you could tag it and search for that checkpoint after the level has loaded.
There's quite a few ways you could set them dynamically, it depends on the result you're ai$$anonymous$$g for.
Not too sure right now. Probably many, but 4-7 per scene only.
Its for entering and exiting. If i enter a house, then exit. i want to spawn outside the door, not at 0,0,0.
Right now i have a string setup, when i trigger a door (load.level) it changes the string to something such as "Spawn2" and when i exit, i want the character to now spawn at "Spawn2".
Playing around with it now, trying to get it working with what you provided initially.
Your answer
Follow this Question
Related Questions
GameObject not working with variable in C# script 3 Answers
change the front point or head of a GameObject 1 Answer
Need workaround for this line of code 1 Answer
Gameobject follow cursor 2D 0 Answers