How do I Add Speed to Instantiated Object.

I created a class of “Racers” in Script ONE, say 100 racers (or whatever)…this is the “Universe” … the class contains attributes such as speed, endurance, drag, etc. Works fine, no problems creating or managing the class.

In Script TWO, I create a sub-class in which I randomly select a half dozen or so of the racers in the Universe to compete in a race. No problems selecting the racers from the Universe and no problems creating the sub class.

This is from Script TWO … notice the last two lines here.

		for (int i = 0; i < runnersInRace; i++)   // instantiate prefab racer
		{

			int tempMeasure = (100-runnersInRace * 5) / 2 + 150; // This section centers racers on the track.
			GameObject cloneCurrentRacer = Instantiate (CurrentRacer);
			cloneCurrentRacer.transform.position = new Vector3(95, 1, tempMeasure - i*5);

			pickNewNumber:  // This section prevents runners from being picked twice by assigning a "-1" to rest.
			int thisPick = Random.Range (0, select.dingy.Count);
			if (select.dingy[thisPick].dRest<0) {goto pickNewNumber;}
			select.dingy [thisPick].dRest = -1;

			racer.Add (new CurrentRace (select.dingy[thisPick].dName, select.dingy[thisPick].dSpeed, select.dingy[thisPick].dDrag, select.dingy[thisPick].dWeight, select.dingy[thisPick].dRest));

			cloneCurrentRacer.GetComponent<Rigidbody> ().drag = select.dingy[thisPick].dDrag;
			cloneCurrentRacer.GetComponent<Rigidbody> ().mass = select.dingy[thisPick].dWeight;

This all works fine when I get over to Script THREE where I actually do the racing. There is nothing to do over there in regard to drag or weight/mass because all that carries over there like magic.

BUT … speed is a different matter. I control the speed of the racers in Script THREE by this:

rb = GetComponent<Rigidbody> ();

thrust = mySpeed;

	void FixedUpdate () 
	{
		rb.AddForce(thrust,0,0, ForceMode.Acceleration);
        }

How do I most easily get the speed component from Script TWO into my thrust component in Script THREE? There isn’t a “speed” component to the Rigidbody. It would be perfect if there was because it would carry forward to here automatically and I would have little to do in that regard. But I’m not aware of any speed component to a Rigidbody. I can assign something to “Thrust” above but it doesn’t carry forward from Script TWO automagically like mass and drag do.

I don’t want to use a for/next loop in my movement script to cycle through the racers and manually convert the speed to force and then insert it into the “Addforce()” above.

Is there some easy way to do this (as with drag and mass) that I am overlooking?

Thanks in advance for any help.

To expand on what @Vyzier said, velocity is probably what you want to effect, velocity is a combination of speed and direction. To make a velocity you can do the following:

float speed = 2;
Vector3 direction = Vector3.forward;
Vector3 velocity = speed * direction;

now you can do as usual:

GetComponent<Rigidbody>().velocity = velocity;

and get racing!

Alternatively if you want to actually pass the speed value through you could add a variable or public setter to ‘Script THREE’ such as:

private float speed;
...
public void SetSpeed(float speed){
    this.speed = speed;
}

and then in Script TWO you could do:

cloneCurrentRacer.GetComponent<Script THREE> ().SetSpeed(select.dingy[thisPick].dSpeed);

Hope that helps.

Alternatively, if you wanted them to travel in different directions, you could use something like this with a nice built-in function:
rb.velocity = Vector3.ClampMagnitude(rb.velocity,speed); .
To get the speed of a current racer, you can use rb.velocity.magnitude.

Although I would recommend making a “racer” script for each racer, you can then individually assign values to each one. That would dive into an idea of object-oriented programming that you may not have done yet