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 RomanorumLegio · Jun 24, 2013 at 11:43 PM · c#errorvariablegetcomponent

Problem calling variable across scripts (Closed)

I need on script, CannonAngle, to respond when the "active" variable from another script, TransformFunctions, returns a one. The TransformFunctions script is on the parent object while the CannonAngle script is attached to one of its child objects. There are no errors, and the Transform is assigned. The problem is that when transformFunction.active should return as 1, the while loop does not commence.

CannonAngle script:

 using UnityEngine;
 using System.Collections;
 
 public class CannonAngle : MonoBehaviour {
     private float angle = 345;
     TransformFunctions transformFunctions;
     public Transform bot;
     
     // Use this for initialization
     void Start () {
         bot = gameObject.GetComponent<Transform>();
         bot = gameObject.transform;
     }
     
     // Update is called once per frame
     void Update () {
         transformFunctions = bot.GetComponent<TransformFunctions>();
         
         while(transformFunctions.active == 1){
             angle = Mathf.Clamp(angle, 330, 355);
             
             if(Input.GetKeyDown(KeyCode.UpArrow)){
                 angle -= 5;
                 transform.localRotation = Quaternion.AngleAxis(angle, Vector3.right);
                 Debug.Log("Angle Increase");
             }
             if(Input.GetKeyDown(KeyCode.DownArrow)){
                 angle += 5;
                 transform.localRotation = Quaternion.AngleAxis(angle, Vector3.right);
                 Debug.Log("Angle Decrease");
             }
         }
     }
 }

TransformFunctions script:

 using UnityEngine;
 using System.Collections;
 
 public class TransformFunctions : MonoBehaviour {
 
     public int active = 0;
     public int activePot = 1;
     private float speed = 22f;
     private float turnSpeed = 2f;
     public float terrain = 1f;
     public double powerUnits = 1000d;
     private float rocketPower = 12000;
     private int shootWait = 10;
     private float rotation;
     private int targetingOverlayToggle = 0;
     public Rigidbody bullet;
     public Rigidbody emptyRocket;
     public Rigidbody emptyBullet;
     public Transform body;
     public Rigidbody explosion;
     public Rigidbody rocket;
     public Transform barrel1End;
     public Transform barrel2End;
     public Transform rocketEnd;
     Vector3 randomRotation;
     Stats stats;
     
     void Start (){
         stats = GetComponent<Stats>();
         randomRotation = new Vector3(Random.Range(-45,45),Random.Range(-45,45),Random.Range(-45,45));
         //call update overlay
         InvokeRepeating("UpdateOverlay", 0, 0.75f);
     }
     
     //toggle actice
     void OnMouseOver(){
         if(Input.GetMouseButtonDown(0)){
             if(active == 0){
                 active = 1;
             }
             else{
                 active = 0;
             }
         }
     }
     
     
     void Update () {
         //kill do death animation and kill script if dead
         if(stats.destroyed == 1){
             Rigidbody explosionInstance;
             rigidbody.AddForce(Vector3.up * 300000);
             rigidbody.AddTorque(randomRotation * 3000);
             explosionInstance = Instantiate(explosion, body.position, body.rotation) as Rigidbody;
             Destroy (this);
         }
         //check if alive
         if(stats.destroyed == 0){
             //check if turn
             if(activePot == 1){
                 //check if set to active state
                 if(active == 1){
                     //check if has power units
                     if(powerUnits > 0){
                         if(targetingOverlayToggle == 0){
                             //look at mouse
                             Plane playerPlane = new Plane(Vector3.up, transform.position);
                         
                             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                         
                             float hitdist = 0.0f;
                             
                             if(playerPlane.Raycast(ray,out hitdist)){
                             
                                 Vector3 targetPoint = ray.GetPoint(hitdist);
                             
                                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                             
                                 transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSpeed*Time.deltaTime);
                             }
                         }
                         
                         //toggle targeting overlay
                         if(Input.GetKeyDown(KeyCode.Mouse2)){
                             if(active == 1){
                                 if(targetingOverlayToggle == 0){
                                     targetingOverlayToggle = 1;
                                 }
                                 else{
                                     targetingOverlayToggle = 0;
                                 }
                             }
                             else{
                                 targetingOverlayToggle = 0;
                             }
                         }
                         
                         
             
                         //move    
                         if(Input.GetKey(KeyCode.Mouse1)){
                             rigidbody.AddForce(transform.forward * speed * terrain * 100 *Time.deltaTime,ForceMode.Acceleration);
                             powerUnits-=2;
                         }
                         
                     }
                     
                     //lower rocketPower
                     if(Input.GetKeyDown(KeyCode.LeftArrow)){
                         rocketPower -= 500;
                     }
                     
                     //increase rocketPower
                     if(Input.GetKeyDown(KeyCode.RightArrow)){
                         rocketPower += 500;
                     }
                     
                     //fire rocket
                     if(powerUnits >= 500){
                         if(Input.GetKeyDown(KeyCode.RightControl)){
                             powerUnits -= 500;
                             Rigidbody rocketInstance;
                             rocketInstance = Instantiate(rocket, rocketEnd.position, rocketEnd.rotation) as Rigidbody;
                             rocketInstance.AddForce(rocketEnd.forward * rocketPower);
                         }
                     }
                             
                     
                     if(powerUnits >= 200){
                         //check for spacebar down
                         if(Input.GetKeyDown(KeyCode.Space)){
                             //lower powerUnits
                             powerUnits -= 200;
                             //Fire ten bullets
                             Rigidbody bulletInstance;
                             bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 9/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             bulletInstance.AddForce(barrel1End.up * 5);
                             bulletInstance = Instantiate(bullet, barrel2End.position, barrel2End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 8/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             //bulletInstance.AddForce(barrel1End.up * 5);
                             shootWait --;    
                                 
                             bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 7/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             bulletInstance.AddForce(barrel1End.up * 5);
                             bulletInstance = Instantiate(bullet, barrel2End.position, barrel2End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 6/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             //bulletInstance.AddForce(barrel1End.up * 5);
                             shootWait --;
                                 
                             bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 5/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             bulletInstance.AddForce(barrel1End.up * 5);
                             bulletInstance = Instantiate(bullet, barrel2End.position, barrel2End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 4/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             //bulletInstance.AddForce(barrel1End.up * 5);
                             shootWait --;
                                 
                             bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             bulletInstance.transform.Translate(Vector3.forward * 3/2);
                             bulletInstance.AddForce(barrel1End.up * 5);
                             bulletInstance = Instantiate(bullet, barrel2End.position, barrel2End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward *2/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             //bulletInstance.AddForce(barrel1End.up * 5);
                             shootWait --;
                                 
                             bulletInstance = Instantiate(bullet, barrel1End.position, barrel1End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 1/2);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             bulletInstance.AddForce(barrel1End.up * 5);
                             bulletInstance = Instantiate(bullet, barrel2End.position, barrel2End.rotation) as Rigidbody;
                             bulletInstance.transform.Translate(Vector3.forward * 1/5);
                             bulletInstance.AddForce(barrel1End.forward * 5000);
                             //bulletInstance.AddForce(barrel1End.up * 5);
                             shootWait --;    
                         }    
                     }
                 }
             }
         }
         //end unit's turn
         if(powerUnits == 0){
             activePot = 0;
             active = 0;
         }
         //set max and minimum rocketPower
         if(rocketPower > 12000){
             rocketPower = 12000;
         }
         if(rocketPower < 5000){
             rocketPower = 5000;
         }
     }
     void UpdateOverlay(){
         //run targeting overlay if applicable
         if(active == 1){
             if(targetingOverlayToggle == 1){
                 if(powerUnits >= 200){
                     if(powerUnits >= 500){
                         Rigidbody rocketTargetingInstance;
                         rocketTargetingInstance = Instantiate(emptyRocket, rocketEnd.position, rocketEnd.rotation) as Rigidbody;
                         rocketTargetingInstance.AddForce(rocketEnd.forward * rocketPower);
                     }
                     Rigidbody bulletTargetingInstance;
                     bulletTargetingInstance = Instantiate(emptyBullet, body.position, body.rotation) as Rigidbody;
                     bulletTargetingInstance.AddForce(body.forward * 5000);
                 }
             }
         }
     }
 }


Thanks in advance.

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

1 Reply

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

Answer by RomanorumLegio · Jun 26, 2013 at 03:36 AM

I found my problem. I didn't properly define the bot GameObject.

Revised CannonAngle script:

 using UnityEngine;
 using System.Collections;
 
 public class CannonAngle : MonoBehaviour {
     private float angle = 345;
     TransformFunctions transformFunctions;
     GameObject bot;
     
     // Use this for initialization
     void Start () {
         GameObject bot = GameObject.FindGameObjectWithTag("bot2");
         transformFunctions = bot.GetComponent<TransformFunctions>();
     }
     
     // Update is called once per frame
     void Update () {
         if(transformFunctions.active == null){
             Debug.Log("Active is Null");
         }
         
         if(transformFunctions.active == 1){
             angle = Mathf.Clamp(angle, 330, 355);
             
             if(Input.GetKeyDown(KeyCode.UpArrow)){
                 angle -= 5;
                 transform.localRotation = Quaternion.AngleAxis(angle, Vector3.right);
                 Debug.Log("Angle Increase");
             }
             if(Input.GetKeyDown(KeyCode.DownArrow)){
                 angle += 5;
                 transform.localRotation = Quaternion.AngleAxis(angle, Vector3.right);
                 Debug.Log("Angle Decrease");
             }
         }
     }
 }
 
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

15 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

Related Questions

error CS0841: A local variable `holdSound' cannot be used before it is declared 1 Answer

GetComponent with variable script possible? 1 Answer

C# getComponent result error cast 3 Answers

Can't import a variable from another script!! 1 Answer

Is it possible to use a variable as a Type? 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