- Home /
Changing a GameObjects Position
How do i change a GameObjects position? I know about transform.position = Vector(X,Y,Z); but these are GameObjects. I want when the user starts the game or a new level the GameObjects are in a certain place. Since the GameObjects will be carrying there values from the other levels they will appear in the same place and i don't want that. Thanks!
the easiest would be to change the positions on level load
basically something like
bool LoadingScene1;
if(LoadingScene1) { transform.postioin = ...; }
HOWEVER THIS IS FOR DYNA$$anonymous$$ICALLY GENERATED STUFF
if you for example place objects in the scene in the unity editor they will have that position. if you start a new scene and place stuff and load that scene it'll of course place those objects where those are.
but yea you just start the load process and at somepoint do a startcoroutine or call a function or change a bool or something and start changing out values. Does that make sense?
Sorry no I'm not completely sure what you mean. The game is like a board game type style so i want each hero to start in a specific place on the board. And since the user picks his 5 heroes those heroes get saved to and array. i want all that data to get saved and carried over to the next level when the user starts the game (I'm using dontdestroyonload), But since i dont know what heroes the user will pick, only the variable names that the heroes will get saved to, i wanted to know if there was something to say This gameobject/variable is suppose to appear here at the beginning of a scene.
Answer by sparkzbarca · Jan 13, 2014 at 12:07 AM
well a hero should probably be a class and its starting position probably be a variable in that class
you'd make a new script called Hero notice its abstract, its not going to be anything but a base its a common list of traits and functions taht all heroes will share.
public abstract class Hero
{
startpos;
health;
attack;
armor;
public attack()
{
//attack
}
public recievedamage(int damage)
{
//take in raw damage modify it based on things
//liek armor then lower hp
}
public gainhp (int hpgain)
{
//take in raw health modify it based on things
//like healing power or soemthing then raise hp
}
void Start()
{
startpos =.......;
}
}
//class ends
next script now the hero class is a generic class basically it just says things about what heroes should have now were going to inherit from it for specific heroes to be made notice the ::Hero that means warrior is a hero and inherits all the things they have we can then go and put in special warrior specific things often by simply defining stuff from hero. For example Hero has armor as a stat but it doesnt say what armor is cause its different presumably for each hero so in here we'll define all those stats in start to pertain to this hero.
class Warrior::Hero()
{
void start()
{
startpos = .....
armor = ....
}
}
you'll have others like
public class ranger::hero
{
..
}
public class mage::hero
{
}
Script where you have 5 hereos picked now we'll create a generic array of hereos.
class Pickscript()
{
hero[] HeroesChosen;
void start()
{
HeroesChosen = new Hero[5];
}
if (pick == 1)
{
HeroesChosen[0] = new Warrior();
}
}
now you have a warrior in the heroes chosen and know everything about him including startpostion
this may seem like way more work but inheritence is VERY importnat and you need to read and learn about it.
beign able later to do things like this.
hero.attack()
and it doesnt matter whether a warrior or a mage or whatever is attacking with each you have redifined attack for each so that you can just type hero.attack and it works regardless of a hero. You can just hero.applydamage(5) and it accounts for armor and stuff thats the only way to make this managable.
Also you dont want to keep redefining stuff things like for example Health. every hero has health so you should only define it once.
take for example if i have a turret with a function that autoaims the turret. I have turret as my base class (a base class is the base its what the other inherit from) then i have 2 turrets, laser turret and pulse laser turret. Now both need an autoaim. well if i define it once in the base and they inherit they can both use it. But if i dont I have to define it twice by copy and pasting code basically into each class and then if i realize there is this problem where autoaim sometimes works wrong i now have to change the code in 2 or maybe 3 or 4 places and this is immpossible to keep track of. Code should never be copy and pasted if your doing that you need to pull out that snippet of code into a function and reuse it so you dont end up worrying about modifying it.
This is alot more than you asked but learning proper coding now will save you heartbreak.
WOW thank you this was very informative and quite helpful, I am very appreciative and this is going to help my program so much! So I'm guessing in the script that pertains to a certain hero the startpos variable would be something like, startpos = Vector3 (X,Y,Z); And then transform.postion = startpos; Would that be correct?
yep thats how it works. post if your having any issues. I have some weapon scripts as well that show inheritance where you go from a turret to a ballistic/energy turret to a torpedo turret so you have the torpedo turret which inherits from ballistic turret which inherits from turret base to show how inheritance progression works.
Your answer
Follow this Question
Related Questions
Empty Parent GameObject Moving with Child 1 Answer
translate 2 Answers
Random Range Position 2 Answers
How to make cubes between 2 point and stick them together? 0 Answers
Two Firing points, Vectors 1 Answer