- Home /
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 [i].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.
Answer by Symphony · Sep 26, 2014 at 03:25 AM
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
Your answer
Follow this Question
Related Questions
Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer
How can i reverse all the booleans in a method? 2 Answers
How would I make 1 bool true whilst making others false 1 Answer
How to schedule responses (based on player's input text) for text adventure game? 0 Answers