THaynes
December 29, 2016, 6:41am
1
I have a script on my player object called DynaControls that has variables that I want to be called from by enemies in my game. So I referenced the DynaControls script in a ESpawnLife script that is on my enemies. Everything seems to be connecting in code, but the variables are not being called in Unity. Nothing referencing ‘DC’ is working.
public class ESpawnLife : MonoBehaviour
{
public GameObject eMine;
public GameObject eBullet;
public GameObject player;
public DynaControls DC;
void Start ()
{
InvokeRepeating("SpawnMines", 2, 5);
InvokeRepeating("SpawnBullets", 2, 10);
DC = player.GetComponent<DynaControls>();
//otherObject = GameObject.FindGameObjectWithTag("Player").GetComponent<GameObject>();
}
void Update ()
{
DC.dead = true;
print("autodeath");
if (DC.deleteAura == true)
{
print("seeing it");
}
}
void SpawnMines()
{
Instantiate(eMine, this.transform.position, Quaternion.identity);
}
void SpawnBullets()
{
Instantiate(eBullet, this.transform.position, Quaternion.identity);
}
void OnCollisionEnter (Collision other)
{
if (other.gameObject.tag == "Player")
{
if (DC.deleteAura || DC.reterAura == true)
{
print("deathtouch");
Destroy(this.gameObject);
}
}
}
}
THaynes
December 29, 2016, 8:21pm
2
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class DynaControls : MonoBehaviour
{
#region Singleton
private static DynaControls _instance;
public static DynaControls Instance;
#endregion
public delegate void OnMessageReceived();
public delegate void SwitchEnemyHue(Color color);
//public static event SwitchEnemyHue onEnemyHit;
public float moveSpeed;
public float rotSpeed;
public Vector3 eularAngleVelocity;
public bool reterAura = false;
public bool deleteAura = false;
public bool dead = false;
public int areaEnemiesDead;
public GameObject reterParticler;
public GameObject deleteParticler;
private Rigidbody rb;
Slider[] HS;
//Image CI;//game over canvas image
void Start ()
{
rb = GetComponent <Rigidbody> ();
HS = GetComponentsInChildren<Slider>();//0=Horo,1=Flak,2=Health
//CI = GetComponentInChildren<Image>();
dead = false;
}
public void DebugMe ()
{
Debug.Log("Called"); //this works fine. It's called as soon as the enemy spawns.
}
void OnCollisionEnter(Collision coll)
{
if (coll.gameObject.tag == "Enemy")
{
if (reterAura == false && deleteAura == false)
{
HS[2].value--;
if (HS[2].value < 1)
{
// Destroy(this);
// dead = true;
}
}
if (reterAura == true)
{
//Destroy(coll.gameObject);
HS[0].value++;
HS[1].value--;
}
if (deleteAura == true)
{
//Destroy(coll.gameObject);
HS[0].value--;
HS[1].value++;
}
}
if (coll.gameObject.tag == "Healer")
{
HS[2].value++;
if (reterAura == true)
//Destroy(coll.gameObject);
HS[0].value++;
}
}
void WriteMessage()
{
}
void FixedUpdate ()
{
float x = (Input.GetAxis ("Horizontal") * moveSpeed * Time.fixedDeltaTime);
float y = (Input.GetAxis ("Vertical") * moveSpeed * Time.fixedDeltaTime);
float z = (Input.GetAxis ("HDepth") * moveSpeed * Time.fixedDeltaTime);
rb.MovePosition (rb.position + (Vector3.right * x) + (Vector3.up * y) + (Vector3.forward * z));
#region //Rotation (works, but is not being used)
// Quaternion deltaRotation = Quaternion.Euler (eularAngleVelocity * Time.deltaTime);
// float xR = (Input.GetAxis ("Shake") * rotSpeed * Time.fixedDeltaTime);
// float yR = (Input.GetAxis ("Nod") * rotSpeed * Time.fixedDeltaTime);
// float zR = (Input.GetAxis ("Tilt") * rotSpeed * Time.fixedDeltaTime);
// rb.MoveRotation (rb.rotation + (Quaternion.Euler * xR) + (Quaternion.Euler * yR) + (Quaternion.Angle * zR));
// transform.Rotate (new Vector3 (xR, yR, zR));
#endregion
if (Input.GetButtonDown("RETER"))
{
reterAura = true;
reterParticler.SetActive(true);
} else if (Input.GetButtonUp("RETER")){ reterAura = false; reterParticler.SetActive(false); }
else if (Input.GetButtonDown("DELETE"))
{
deleteAura = true;
deleteParticler.SetActive(true);
}
else if (Input.GetButtonUp("DELETE")) { deleteAura = false; deleteParticler.SetActive(false); }
}
//void Death ()
//{
// if (dead == true)
// {
// CI.enabled = true;
// }
//}
}