0

In the Hierarchy i have two ThirdPersonController The script is attached to each one of them

Then i went to the ThirdPersonController script and changed the speed variable to be public:

[SerializeField] public float m_MoveSpeedMultiplier = 1f;

Then in my script top i did:

public float walkSpeed = 10f;
private ThirdPersonCharacter[] thirdPersonCharacter;

Then in the Awake

void Awake()
{
        thirdPersonCharacter = new ThirdPersonCharacter[2];
        for(int i = 0; i < thirdPersonCharacter.Length; i++)
        {
            thirdPersonCharacter[i] = GetComponent<ThirdPersonCharacter>();
        }
}

Then in the Update

void Update()
    {
        thirdPersonCharacter[0].m_MoveSpeedMultiplier = walkSpeed;
        thirdPersonCharacter[1].m_MoveSpeedMultiplier = walkSpeed;
    }

But when the game is running and i'm changing in one of the ThirdPersonController in the Inspector in the script the walkSpeed value from 10 to 1 or from 10 to 20 it's effecting only one of the characters. Why it's not effecting both of them ?

Since it didn't work i tried:

public ThirdPersonCharacter[] thirdPersonCharacter;

Then in the Start function removed the code before. And just dragged the ThirdPersonController and the ThirdPersoncontroller(1) to the Inspector in the script after changed the size of thirdPersonCharacter to 2. The script now is attached only to the ThirdPersonController.

And still when changing the speed value while the is running it's effecting only one of them only the ThirdPersonController.

My general goal is to be able to controll both ThirdPersonController and ThirdPersonController(1) same speed in real time while the game is running and then also to be able to controll each one speed in seperate. So i will have later 3 values of speed. One for both and two for each one. But i can't yet make them both speed to be changed.

Daniel Halfoni
  • 487
  • 10
  • 30

2 Answers2

1

The problem is similar to the problem from this post.

The line of code below:

thirdPersonCharacter[i] = GetComponent<ThirdPersonCharacter>();

will return reference of ThirdPersonCharacter attached to the GameObject this script is attached to. It will return the-same reference twice in the for loop.

To get different ThirdPersonCharacter, you have to find each individual GameObject the ThirdPersonCharacter is attached to then perform GetComponent on them.

GameObject tpc1 = GameObject.Find("TPC1");
thirdPersonCharacter[0] = tpc1.GetComponent<ThirdPersonCharacter>();

GameObject tpc2 = GameObject.Find("TPC2");
thirdPersonCharacter[1] = tpc2.GetComponent<ThirdPersonCharacter>();

Replace TPC1 and TPC2 with the name of GameObjects ThirdPersonCharacter is attached to.

You can find other ways to do this here.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
0
thirdPersonCharacter[i] = GetComponent<ThirdPersonCharacter>();

The above code returns the same (first) component each time. Try this instead;

void Awake()
{
    thirdPersonCharacter = GetComponents<ThirdPersonCharacter>();
}
Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • This is another way to do it. Note that if you do it this way, you have to do extra work like checking the gameobject name if you care about the order of the `ThirdPersonCharacter` returned. Otherwise, it may return `ThirdPersonCharacter1` as first index this time and anytime in your game, it might return `ThirdPersonCharacter2` first. – Programmer Feb 20 '17 at 11:41