- Home /
Question by
AwesumeOne · Dec 09, 2013 at 01:37 AM ·
variables
Click sphere for variables
I have a "galaxy" of spheres for suns and planets. I am trying to get it so that when you click a planet the script running for that game object sends its soldier count to another script where I can open a gui window. I have searched and searched for three days and still can't get it right, so if anyone is able to help I would be very grateful. Here are the two script codes. This is the code for the camera which is actually handling the GUI.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraZoom : MonoBehaviour {
private float smooth = 1f;
private Ray mouseRay;
private Transform camTransform;
private int camParse;
private Vector3 targetPos;
private Vector3 currentPos;
private Vector3 solarPos;
private bool isMoving;
private RaycastHit mouseHit;
private int viewLevel;
private int rayLayer;
public Texture redBar;
public Texture blueBar;
public Texture greenBar;
public Texture yellowBar;
private GameObject selectedPlanet;
void Start()
{
//Always cache components that you will use frequently, it's much more efficient :)
camTransform = Camera.main.transform;
//Initialize target position
currentPos = camTransform.position;
isMoving = false;
viewLevel = 1; //3 = planet, 2 = solar, 1 = glaxy
targetPos = new Vector3(-75, 0 ,-110);
}
// Update is called once per frame
void Update()
{
currentPos = camTransform.position;
mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(mouseRay, out mouseHit);
while(isMoving == false) //is the camera moving?
{
switch (viewLevel)
{
case 3: // planet view (no more zoom in, so only zoom out code)
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
targetPos = solarPos;
viewLevel = 2;
isMoving = false;
smooth = 0.25f;
}
break;
case 2: // solar view (zoom to either planet or galaxy view)
if(Input.GetAxis("Mouse ScrollWheel") < 0) //scroll back
{
targetPos = new Vector3(-75, 0 ,-110);
viewLevel = 1;
isMoving = true;
smooth = 1.5f;
}
if(Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
{
if(mouseHit.collider != null)
{
targetPos = new Vector3(mouseHit.collider.gameObject.transform.position.x, mouseHit.collider.gameObject.transform.position.y, -1.5f);
solarPos = camTransform.position;
viewLevel = 3;
isMoving = true;
smooth = 0.25f;
selectedPlanet = mouseHit.collider.gameObject.GetComponent();
}
}
break;
case 1: // galaxy view (no more zoom out, so only zoom in code)
if(Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
{
if(mouseHit.collider != null)
{
targetPos = new Vector3(mouseHit.collider.transform.position.x + 15, mouseHit.collider.transform.position.y, -10); viewLevel = 2;
isMoving = true;
smooth = 1.5f;
}
}
break;
default:
break;
}
break;
}
if(currentPos == targetPos)
{
isMoving = false;
}
camTransform.position = Vector3.MoveTowards(currentPos, targetPos, smooth);
}
private void OnGUI()
{
if(viewLevel == 3)
{
if(GUI.Button(new Rect(0,0, 175, 40), "Military Personnel"))
{
}
GUI.Box(new Rect(175, 0, 175, 40), "Offical Personnel");
GUI.Box(new Rect(Screen.width - 350, 0, 175, 40), "Military Buildings");
GUI.Box(new Rect(Screen.width - 175, 0, 175, 40), "Ships");
//add progress bars of factions
GUI.DrawTexture(new Rect(Screen.width / 2 - 50, 0, 100, 10), redBar);
GUI.DrawTexture(new Rect(Screen.width / 2 - 50, 10, 100, 10), blueBar);
GUI.DrawTexture(new Rect(Screen.width / 2 - 50, 20, 100, 10), greenBar);
GUI.DrawTexture(new Rect(Screen.width / 2 - 50, 30, 100, 10), yellowBar);
}
}
void SoldierFunction()
{
}
}
using UnityEngine;
using System.Collections;
public class PlanetStats : MonoBehaviour {
//declare ~all~ variables, then initialize them.
public int planetSoldiers1;
public int planetSoldiers2;
public int planetSoldiers3;
public int planetSoldiers4;
public int planetShips1;
public int planetShips2;
public int planetShips3;
public int planetShips4;
public int planetPersonel1;
public int planetPersonel2;
public int planetPersonel3;
public int planetPersonel4;
public float planetFaction1;
public float planetFaction2;
public float planetFaction3;
public float planetFaction4;
public int trueFaction;
public float planetOre;
public float planetSynth;
public float planetEnergy;
public int wholeOre;
public int wholeSynth;
public int wholeEnergy;
// Use this for initialization
void Start ()
{
planetSoldiers1 = 0;
planetSoldiers2 = 0;
planetSoldiers3 = 0;
planetSoldiers4 = 0;
planetShips1 = 0;
planetShips2 = 0;
planetShips3 = 0;
planetShips4 = 0;
planetPersonel1 = 0;
planetPersonel2 = 0;
planetPersonel3 = 0;
planetPersonel4 = 0;
planetFaction1 = 25.0f;
planetFaction2 = 25.0f;
planetFaction3 = 25.0f;
planetFaction4 = 25.0f;
planetOre = 0.001f;
planetSynth = 0.001f;
planetEnergy = 0.001f;
trueFaction = 0;
}
// Update is called once per frame
void Update ()
{
//generate the resources of the planet
planetOre += 0.01f;
planetSynth += 0.01f;
planetEnergy += 0.01f;
//round the resources to whole number
wholeOre = Mathf.RoundToInt(planetOre);
wholeSynth = Mathf.RoundToInt(planetSynth);
wholeEnergy = Mathf.RoundToInt(planetEnergy);
//set who owns the planet
if(planetFaction1 > planetFaction2 || planetFaction1 > planetFaction3 || planetFaction1 > planetFaction4)
{
trueFaction = 1;
}
if(planetFaction2 > planetFaction1 || planetFaction2 > planetFaction3 || planetFaction2 > planetFaction4)
{
trueFaction = 2;
}
if(planetFaction3 > planetFaction1 || planetFaction3 > planetFaction2 || planetFaction3 > planetFaction4)
{
trueFaction = 3;
}
if(planetFaction4 > planetFaction1 || planetFaction4 > planetFaction2 || planetFaction4 > planetFaction3)
{
trueFaction = 4;
}
}
}
That was the code for the planets. The "planetSoldiers1" is the variable I'm trying to pass to the camera code.
Comment
Ins$$anonymous$$d of writing int and other variables 100 times use arrays.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123734im_/https://answers.unity.com/themes/thub/images/avi.jpg)