Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SuperPurpleByte · Sep 20, 2014 at 02:16 AM · booleancommunicationrocket

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperPurpleByte · Sep 20, 2014 at 12:41 PM 0
Share

Thanks a ton :)

avatar image dmg0600 · Sep 21, 2014 at 09:12 AM 0
Share

Gald to help, @SuperPurpleByte ! If you can, please accept the answer

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to know which script you change? 3 Answers

SendMessage and boolean... my GO doesn't keep the value. 2 Answers

Can someone translate C# to Javascript? 2 Answers

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

Boolean while looking at a game object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges