- Home /
Script in UNITY ???
I have some question on scripting in unity. I am learning and i hope some one can clear this out the way so i can keep learning more. at the moment how i am using my scripts is i simple write it, and attach it to my object.
is there another way to do this where I can control multiple objects in my scene with just one large code attached to (????)....
lets say i have a ball that has animations, and there will be triggers in my scene(Cube). the ball will animate passing trough it, once triggered it will start an other animation of an other object in the scene, or start a particle or even bring up text.
how about if i want to have a set resolution of my game, so the player has only one option when the build is created. I am pretty sure that i can't write my code and drop it. There needs to be a other way.
Answer by MibZ · Nov 19, 2012 at 11:48 PM
You DON'T need multiple scripts to control multiple objects. If you wanted to you could control an entire game through one script. (Minus all of the Unity under the hood of course, but that's already programmed for you.)
If you wanted to you could even have one object controlled by multiple scripts, just make sure you don't overcomplicate it and lose track of how things work.
If you only want to do simple changes to objects (move, rotate, scale, change texture in runtime, etc) you can use a GameObject reference. This can be done two ways, the Designer friendly way and the Programmer friendly way.
//"Designer method"
public GameObject sphereToMove;
//Public variables can be seen and changed in the Inspector tab. To assign a GameObject as sphereToMove, drag the GameObject you want from your Hierarchy tab into the variable in the Inspector.
//This method is nice because it lets you very easily change which GameObject is effected by your script without having to change any code.
//"Programmer method"
public GameObject sphereToMove = GameObject.Find("Basketball");
//I still used 'public' accessibility, but it can also be 'private'.
//This method is nice for when you have a lot of objects in your scene, or for when you know that a script will only be used to effect one object. (For example, your Player script will only effect players, and if you are making a single player game you will only ever have one player.)
To use this to make your script effect more than one object, you need to create a GameObject variable for each individual object you want to move. If you have more than a handfull of them I would recommend reading up on Arrays and Lists.
If you want to effect more than one object in complicated ways (For example, you want to add 10 health to every enemy in your scene) you need to have a script reference. Script references can be made for any script, whether that is a script that comes with Unity (Like Spring Joints or Character Controllers) or a script that you have written yourself.
To declare a script reference, use the name of the script as the Type.
//NOTE: Accessing a script requires one more step than finding a GameObject, you need to use the [GetComponent][3] function like so.
public GunTurret turret = GameObject.Find("Turret").GetComponent<GunTurret>();
[3]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
Answer by Cool Dave · Nov 19, 2012 at 11:09 PM
Don't know what you're asking but here is link to an answer for a question you might be asking.
http://answers.unity3d.com/questions/316035/very-simple-question.html
The second question...
Edit>Project Settings>Player>PC and Mac TAB>Display Resolution Dialog
It's in the only sensible place...the player settings.
Try to ask one question per post. And writing in proper English is preferred.
Hope this helps!
Dave