3. Locate by name or tag.
You can use gameobject. findwithtag and gameobject. findgameobjectswithtag to search for game objects with specific tags and use gameobject. Find to search for objects by name.
Function start ()
{
// By name
VaR go = gameobject. Find ("someguy ");
Go. Transform. Translate (0, 1, 0 );
// Follow the label
VaR player = gameobject. findwithtag ("Player ");
Player. Transform. Translate (0, 1, 0 );
}
You can use getcomponent In the result to get any script or component on the game object you find.
Function start ()
{
// By name
VaR go = gameobject. Find ("someguy ");
Go. getcomponent (otherscript). dosomething ();
// By tag
VaR player = gameobject. findwithtag ("Player ");
Player. getcomponent (otherscript). dosomething ();
}
Some special objects have shortcuts. For example, the main camera uses camera. Main.
4. pass as a parameter
Some event messages include detailed information in the event. For example, trigger events pass the collider component of the collision object to the processing function.
Ontriggerstay gives us a reference to the collision generator. From this collision tool, we can obtain the rigid body attached to it.
Function ontriggerstay (Other: Collider ){
// If the other collision tool also has a Rigid Body
// Apply a force to it
If (other. Rigidbody ){
Other. Rigidbody. addforce (0, 2, 0 );
}
}
Or we can use the collision tool to obtain any component attached to the same object.
Function ontriggerstay (Other: Collider ){
// If otherscript is attached to another collision Generator
// Call the dosomething
// Most of the time, the collision server does not append scripts
// So we need to check first to avoid null reference exceptions.
If (other. getcomponent (otherscript )){
Other. getcomponent (otherscript). dosomething ();
}
}
Note that using the other variable in the above example, you can access any component in the collision object.
5. All types of scripts
Use object. findobjectsoftype to find all objects with the same class or script name, or use object. findobjectoftype. To find the first object of this type.
Function start ()
{
// Find any game object appended with otherscript in the scenario
VaR other: otherscript = findobjectoftype (otherscript );
Other. dosomething ();
}
Overview: Vector
Unity uses the same vector3 class to represent all 3D vectors. Different components of 3D vectors can be accessed by thinking about the X, Y, and Z member variables.
VaR aposition: vector3;
Aposition. x = 1;
Aposition. y = 1;
Aposition. z = 1;
You can also use the vector3 constructor to initialize all components at the same time.
VaR aposition = vector3 (1, 1, 1 );
Vector3 also defines some common variable values.
VaR direction = vector3.up; // same as vector3 (0, 1, 0 );
Operations on a single vector can be accessed using the following methods:
Somevector. normalize ();
The number of vector3 classes can be used for operations using multiple vectors;
Thedistance = vector3.distance (onevector, othervector );
(Note that you must write vector3 before the function name to tell JavaScript where to find this function. This applies to all class functions)
You can also use normal mathematical operations to manipulate vectors.
Combined = vector1 + vector2;
View vector3 documents to obtain a list of complete operations and available attributes.
Overview: member variables & global variables
A variable defined outside of any function is a member variable. In unity, this variable can be accessed through the view panel. Any value saved in the member variable can be automatically saved with the project.
VaR membervariable = 0.0;
The preceding variable is displayed as a value attribute named "member variable" in the view panel.
If you set the variable type to a component type (such as transform, Rigidbody, collider, or any script name), then you can drag a game object in the view panel to set them.
VaR enemy: transform;
Function Update ()
{
If (vector3.distance (enemy. Position, transform. Position) <10 );
Print ("I sense the enemy is near! ");
}
}
You can also create private member variables. Private member variables can be used to store states that are invisible outside the script. Private member variables are not saved to the disk and cannot be edited in the view panel. When it is set to debug mode, they are visible in the view panel. This allows you to use private variables just like a real-time update debugger.
Private var lastcollider: collider;
Function oncollisionenter (collisioninfo: collision ){
Lastcollider = collisioninfo. Other;
}
Global Variables
You can also use the static keyword to create a global variable.
This creates a global variable named someglobal.
// A static variable in 'thescriptname. js'
Static Var someglobal = 5;
// You can access it like a common variable in the script
Print (someglobal );
Someglobal = 1;
To access it from another script, you need to add a vertex and a global variable name to the script name.
Print (thescriptname. someglobal );
Thescriptname. someglobal = 10;