- Home /
How to change the bool values in the rocket? Getting game objects to communicate
using UnityEngine;
using System.Collections;
public class RocketPropulsion : MonoBehaviour {
public float speed; //meterspersecond
public float burnTime;//How long is the rocket have thrust for?
public float spinRate;//How many rotations per second
public float velocityAtLauncherExit;//how fast does it exit the launcher
public Rigidbody rocket;//rocket rigidbody
public string firingMethod;//what input axis is being used?
public float lifeTime;
public Object toBeDestroyed;
public Transform flameStart;// transform for where to instantiate the rocket blast particles
public bool weaponSafe = true;
//Technical data for the rocketMotor
void FixedUpdate(){
if (weaponSafe = false) //This bit is firing the rocket for the burn time
{
while ( burnTime > lifeTime )
{
Rocket ();
lifeTime = lifeTime + 1 * Time.deltaTime;
}
}
if ( burnTime < lifeTime )//orienting the rocket
{
transform.rotation = Quaternion.LookRotation(rigidbody.velocity) * Quaternion.Euler (90,0,0);
//This part is making the rotation equal to the velocity of the rigidbody, the * Quaternion.Euler (90,0,0) is to adjust the rockets orientation from facing vertical to horizonal
}
}
// Use this for initialization
void Start ()
{
rigidbody.isKinematic = true;
}
// Update is called once per frame
void Rocket ()
{
rigidbody.isKinematic = false;
rocket.rigidbody.AddForce(rocket.transform.up * speed * Time.deltaTime, ForceMode.Impulse);//fires the rocket at the defined speed going in meters per second.
}
//this will make the rocket destroy any object tagged "Destroyable" You can change the tag out for anything it is just set it
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Destroyable")
Destroy (toBeDestroyed);
lifeTime = 0; // reset it
//Instantiate(spawnBonus, transform.position, transform.rotation);
}
}
using UnityEngine; using System.Collections;
public class MissileFireSystem : MonoBehaviour {
public GameObject[] rockets;
public string firingMethod;//what input axis is being used?
public GameObject rocket;
public int rocketToFire = 0;//refers to the rocket in the array, 0 means the first rocket
public RocketPropulsion RocketPropulsion;
// Use this for initialization
void Start () {
rockets = GameObject.FindGameObjectsWithTag("rocket#1");//getting all of the rockets, rocket#1 refers to the hydra 70 rockets
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown (firingMethod))
{
for(int i = -1; i < rocketToFire; i++)
{
rockets[rocketToFire].weaponSafe = false; //Fire rocket
rocketToFire = rocketToFire + 1;//This will move on the the next rocket.
}
}
}
}
My goal is to have the second script be able to change all the rockets and fire them via the array, and by taking off there "saftey" bool. Not sure why it wont work, specificaly the "rockets[rocketToFire].weaponSafe = false;" line
I am really not sure what to do to fix it.
Answer by dmg0600 · Sep 20, 2014 at 12:01 PM
You are trying to access variables in your script via GameObject. That is not correct as GameObject does not contain any of the variables of your script. You must first get the component itself.
I have cleaned the code and reformatted the for loop.
using UnityEngine;
using System.Collections;
public class MissileFireSystem : MonoBehaviour
{
public GameObject[] rockets;
public string firingMethod;//what input axis is being used?
// Use this for initialization
void Start()
{
rockets = GameObject.FindGameObjectsWithTag("rocket#1");//getting all of the rockets, rocket#1 refers to the hydra 70 rockets
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown(firingMethod))
{
for (int i = 0; i < rockets.Length; i++)
{
RocketPropulsion rocketComponent = rockets[i].GetComponent<RocketPropulsion>();
if (rocketComponent != null)
{
rocketComponent.weaponSafe = false; //Fire rocket
}
}
}
}
}
The for loop is used to go over the array elements, therefore it should go from 0 (index of the first element of the array) to rockets.Length - 1 (index of the last element of the array).
Gald to help, @SuperPurpleByte ! If you can, please accept the answer
Answer by Grim_Darknight · Sep 20, 2014 at 03:08 AM
for(int i = -1; i < rocketToFire; i++)
according to your explanation maybe try:
for(int i = -1; i < rockets.Count; i++)
also I would suggest adding in a few
Debug.Log();
lines into your code when stuff is supposed to happen.
Answer by SuperPurpleByte · Sep 20, 2014 at 11:43 AM
this the error.
Assets/MissileFireSystem.cs(26,55): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `weaponSafe' and no extension method `weaponSafe' of type `UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)