Sending value stored in an array to another script with cSharp

Using cSharp, with Player001Controls.cs I am sending the value shootForce stored in an array to another script MoveMissile.cs .
The array element is determined by missileSelection. I have it working with this :

Player001Controls.cs

public class Bullet {
  public int missileSelection; 	// Current missile selected
  public int missileVariety;	// Total types of missile variety (max size of array)
  public float[] shotForce; 
  public void InitializeBullets() 
  { 
        missileSelection = 0;
        missileVariety = 4;
        shotForce = new float[missileVariety];
       	shotForce[0] = 1.5f;
     	shotForce[1] = 20f;
    	shotForce[2] = 30f;
    	shotForce[3] = 40f;
    	shootForce = shotForce[missileSelection]; 
  }
}

missileSelection is called from a method in class Player001Controls and the value determined by user input (right mouse button click increments missileSelection) and constrained to array size of missileVariety. However code as it is means of course that missileSelection is 0 whenever shootForce is sent or read from.

I have it working in class Player001Controls with :

public class Player001Controls : MonoBehaviour
{   
    public Bullet B = new Bullet();
	public void Awake()
	{
		B.InitializeBullets();
	}
	
	public void OnGUI() 
	{
			GUI.Label(new Rect (10, 60, 300, 20),"Power of shot: " + B.powerOfShot);
    }
}

Where would I put shootForce = shotForce[missileSelection] in order for it to read the shotForce array stored in InitializeBullets() and return a value determined by pointer missileSelection to another script (mine is called moveMissiles.cs) ?

Ok, from a design point it looks strange to have a bullet class (which obviously represents a single bullet) and see a member function InitializeBullets. I guess the shotForce array should be the same for each bullet? That’s usually something where you could use static variables. static variables are tied to the class and not a single instance, so they are the same for each bullet.

public class Bullet
{
    // member variables / functions
    public int type;  // bullet type
    public float ShootForce
    {
        get
        {
            if (type < 0 || type >= missileVariety)
                return 0.0f;  // Maybe print a warning with Debug.LogWarning()
            return shotForce[type];
        }
    }
    // constructor
    public Bullet(int aBulletType)
    {
        type = aBulletType;
    }


    // static variables / functions
    public static int missileVariety;    // Total types of missile variety (max size of array)
    public static float[] shotForce; 
    public static void InitializeBullets() 
    { 
        missileVariety = 4;
        shotForce = new float[missileVariety];
        shotForce[0] = 1.5f;
        shotForce[1] = 20f;
        shotForce[2] = 30f;
        shotForce[3] = 40f;
    }
}

To initialize the static variables just call InitializeBullets once. Now when you create an instance of your bullet you have to specify the type. The ShootForce property will return the required force according to the bullet type

public class Player001Controls : MonoBehaviour
{   
    public int currentMissile = 0;
    public void Awake()
    {
        Bullet.InitializeBullets();
    }

    public void Update() 
    {
        if (Input.GetMouseButtonDown(0))
        {
            Bullet B = new Bullet(currentMissile);
            // Use B.ShootForce for the new bullet
        }
    }
}

This is a typical OOP example. However, in Unity you usually have MonoBehaviours which are attached to GameObjects. Those scripts / classes can’t be created with new and can’t have a custom constructor. If you want to hold a seperate instance for the bullet you can do it that way i’ve mentioned, but it doesn’t make much sense. Usually you want the class to represent a bullet during it’s whole lifetime. Your bullets are usually prefabs (pre designed GameObjects) which are used to Instantiate (clone) a new bullet when needed. To support different bullets you would create an array of prefabs so you can easily select one of them. The different attributed of each bullet should be handled by a script on each bullet prefab. A simple example:

// Bullet.cs
public class Bullet : MonoBehaviour
{
    public float launchForce; // assigned in the inspector
    void Start()
    {
        rigidbody.AddForce(0,0,launchForce); // add the force in forward direction
    }
}

// Player.cs
public class Player : MonoBehaviour
{
    public Bullet[] bullets; // assign the different bullet prefabs to this array
    public Transform bulletSpawnPoint; // assign the spawnpoint from your weapon
    public int currentBullet = 0;
    public void Shot()
    {
        Instantiate(bullets[currentBullet], bulletSpawnPoint.position, bulletSpawnPoint.rotation);
    }
}

Here is an example of how you access an array from one script from another script.

// file name: ScriptOne.js

var theArray : float [] =  new float [];

function Awake ()
{
    theArray = [ 3.14, 2.71, .074, .999 ];
}

// file name: ScriptTwo.js

/* 
 * you can assign the value of this variable to an instance
 * of a ScriptOne object by dragging and dropping an object 
 * that has a ScriptOne attached to it into the Inspector onto
 * the slot named "Instance Of ScriptOne" after selecting an
 * object that has a "ScriptTwo" attached to it
 *
 *  Step 1: attach this script to an object
 *  Step 2: select the object by left-clicking it
 *  Step 3: look in the Inspector
 *  Step 4: find a slot named "Instance Of Script One"
 *  Step 5: drag an object with a ScriptOne attached to it onto the slot
 *
 */

var instanceOfScriptOne : ScriptOne;

function Start () 
{
    /* check to see if you read my comment above */

    if ( ! instanceOfScriptOne ) {
        Debug.LogWarning("Please assign the instance in the Inspector");
        return;
    }

    /* print some values in the array from the ScriptOne script */

    for ( var i : int = 0; i < 10; i ++ ) {
        print(instanceOfScriptOne.theArray*);*
 *}*
*}*
*```*

Here is how I got the value for shootSpeed sent from Player001Controls.cs to MoveMissile.cs

Player001Controls.cs

public class Player001Controls : MonoBehaviour
{ 
    public float shootSpeed
    {
    	get{_shootSpeed = Bullet.shotForce[missileSelection];
    		return _shootSpeed;}
    	set{_shootSpeed = value;}
    }
    private float _shootSpeed;

    public void Awake()
	{		
		Bullet.InitializePlayerBullets();
	}
}

MoveMissile.cs

public class MoveMissile : MonoBehaviour
{
    private float missileSpeed; 

    public void Awake()
    {
        Player001Controls PC = (Player001Controls)GameObject.Find("Player001Character").GetComponent("Player001Controls");
        missileSpeed = PC.shootSpeed;
     }

     void FixedUpdate () 
     {

     	rigidbody.velocity = transform.up * missileSpeed;
     }
}

Player001Controls.cs is attached to the Player001Character object.

MoveMissile.cs is attached to the missile object that is fired by Player001Character