In the Hierarchy i have 2 ThirdPersonController. In the Window > Animator i created new empty state called it Walk and set it to HumanoidWalk so when running the game both players are walking.
On one of them i added the script and as Prefab the second ThirdPersonController(1).
Then when running the game it's making clones of the ThirdPersonController(1). So i see in the Hierarchy more N ThirdPersoncontrollers.
Today to change the speed of walking for each ThirdPersonController i change in the Inspector the Move Speed Multiplier. But if i want in the script already when creating the clones to set to each one another speed how can i do it ?
using UnityEngine;
using System.Collections;
public class Multiple_objects : MonoBehaviour {
public GameObject prefab;
public GameObject[] gos;
public int NumberOfObjects;
void Awake()
{
gos = new GameObject[NumberOfObjects];
for(int i = 0; i < gos.Length; i++)
{
GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
gos [i] = clone;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
What i tried now is to get the Animator component of the Prefab and set the speed to all the clones:
using UnityEngine;
using System.Collections;
public class Multiple_objects : MonoBehaviour {
public GameObject prefab;
public GameObject[] gos;
public int NumberOfObjects;
private Animator _animaotr;
void Awake()
{
gos = new GameObject[NumberOfObjects];
for(int i = 0; i < gos.Length; i++)
{
GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
gos [i] = clone;
_animaotr.speed = 10;
}
}
// Use this for initialization
void Start () {
_animaotr = prefab.GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
}
}
But the main problem is that on my first ThirdPersonController in the Hierarchy the original one i created in Window > Animator empty state called it Walk and set HumandoidWalk.
Now to set the speed for some reason changing the Animator speed never effect of anything for example:
_animaotr.speed = 10;
Only when changing the speed in the ThirdPersonController > Inspector > Third Person Character (Script) > Move Speed Multiplier. And it's changing the same speed to all ThirdPersoncontrollers in the Hierarchy including this i clone.
But how do i change each clone speed to another vlaue of speed ? And why the _animator.speed not changing anything and i need to use this Move Speed Multiplier ?
The Move Speed Multiplier property that is showing in the Editor is declared as m_MoveSpeedMultiplier
in the ThirdPersonCharacter
script. It is delacre as float m_MoveSpeedMultiplier = 1f;
which means that it is a private
variable and cannot be accessed from another script. The reason it is showing up in the Editor is because it has [SerializeField]
on top of it which means that it is a serialized private
variable.
To access it during run-time, you have to change from float m_MoveSpeedMultiplier = 1f;
to public float m_MoveSpeedMultiplier = 1f;
in the ThirdPersonCharacter
script.
Use GetComponent
to get instance of ThirdPersonCharacter
from the gos GameObject then save it somewhere for re-usual. Since you have 2 ThirdPersonCharacter
, you can create two ThirdPersonCharacter
arrays to hold those instances. It should look like the code below:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.ThirdPerson;
public class Multiple_objects : MonoBehaviour
{
public GameObject prefab;
public GameObject[] gos;
public int NumberOfObjects;
private ThirdPersonCharacter[] thirdPersonCharacter;
void Awake()
{
thirdPersonCharacter = new ThirdPersonCharacter[2];
gos = new GameObject[NumberOfObjects];
for (int i = 0; i < gos.Length; i++)
{
GameObject clone = (GameObject)Instantiate(prefab, Vector3.zero, Quaternion.identity);
gos[i] = clone;
thirdPersonCharacter[i] = clone.GetComponent<ThirdPersonCharacter>();
}
}
// Use this for initialization
void Start()
{
thirdPersonCharacter[0].m_MoveSpeedMultiplier = 5f;
thirdPersonCharacter[1].m_MoveSpeedMultiplier = 5f;
}
// Update is called once per frame
void Update()
{
}
}