How to change bool values from one controlling script to a receiver?

I Have a bool on a rocket that when false it will fire the rocket. Then I have a control script that will index all the rockets with a specific tag in to an array. The problem I am having is changing the value of the bool from the control .

This is what I have so far

using UnityEngine;
using System.Collections;

public class MissileFireSystem : MonoBehaviour
{
	public GameObject[] rockets;
	public string firingMethod;//what input axis is being used?
	public int rocketToFire;
	
	// Use this for initialization
	void Start()
	{
		rockets = GameObject.FindGameObjectsWithTag("rocket#1");//getting all of the rockets, rocket#1 refers to the hydra 70 rockets 

			for (int i=0; i>rockets.Length; i++) 
				{
				RocketPropulsion rocketComponent = rockets *.GetComponent<RocketPropulsion> ();*
  •  		}*
    
  • }*

  • // Update is called once per frame*

  • void Update()*

  • {*

  •  if (Input.GetButtonDown(firingMethod))*
    
  •  {*
    
  •  	rockets[rocketToFire].rocketComponent.weaponSafe = false;//this will fire the rocket*
    
  •  	print (rockets[rocketToFire]);*
    
  •  	rocketToFire = rocketToFire + 1;*
    
  •  }*
    
  • }*
    }
    On the second script is just
    when weaponSafe = false fire the rocket.

1 Answer

1

Hi

You need to access your second script within your MissileFireSystem script. An easy way would be to make the bool static then you can just access it with something like

scriptname.scriptvariable

In your case secondscript.weaponSafe *This only works with static variable…and could become a problem if you need to access that bool anywhere else

The other way would be to something like

Access the second script by making a variable for it ex: private secondscript

Then assign it to the appropriate script ex: secondscript = Find the gameObject

Then you can just access it variables like before secondscript.weaponSafe

Note: I’m not a programmer at ALL, i’m still learning myself. So I invite anyone to correct me if i’m wrong…not trying to lead anyone astray

Hope that helps

Thanks ! Helps a ton

Not a problem, happy to help