How can i change each gameobject movement speed?

我的梦境 提交于 2019-12-02 13:22:05

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()
    {

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!