basically i want to change the values of distance and height in smooth follow script when i will Change the target object in game play.
Changing other components’ properties is a very important thing in Unity. Basically, you have three steps.
- Get a reference to the GameObject that should be changed. You do this by either putting your script on the very GameObject, or maybe make a public variable where you can drag and drop the other object into in the editor. There are more ways than this, and what you choose should depend on the situation.
- Use GetComponent to get a reference of the component that you want to change on that GameObject. This step can be skipped if you are directly referencing the component.
- Change a variable value on the component.
For example: If your script and the component you want to change are on the same GameObject, you can skip step one and just say “GetComponent”. The proper way to do this is with component caching, aka finding the component once and saving the reference in a variable, like this:
private NameOfTheOtherScript otherScript;
void Awake()
{
otherScript = GetComponent<NameOfTheOtherScript>();
}
Then, at any other point in the code, you can change a value of that component:
otherScript.someProperty = someValue;
The name of the variable can be seen by either opening the other script’s code or by looking at the inspector. If the inspector says “Movement Speed”, the variable name is probably “movementSpeed”. Spaces removed, first letter lowercase.
So I don’t have the standard assets smooth follow script in mind right now, but it should look somewhat like this:
private SmoothFollow follow;
void Awake()
{
follow = GetComponent<SmoothFollow>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space)) // Just as an example
{
follow.distance = 10f;
}
}