- Home /
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
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");
}
}
}
}