I’ve tried on various occasion for several hours at a time to get access to variables from one script to another, I’ve followed tutorials that have allowed me do this as well as having gone over the documentation on multiple occasions, but I still haven’t managed to figure it out for my own uses. For whatever god awful reason, I just can’t seem to grasp getting information in between scripts unless someone is telling me step by step how to do it.
I have 3 scripts that all work exactly as I need them to, but now I need them to be able to interact. It seems to me that I should be able to somehow call ‘shields.modifyShields’ from the shieldVar script inside DestroyByContact and leave the third script out of it altogether since shieldsVar already only makes its functions happen inside another script called shieldStrength
1)DestroyByContact - A script which watches for collisions and responds accordingly. ((This is actually modified from the ‘Space Shooter’ Unity Tutorial
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour
{
public bool shielded;
public GameObject asterExplode;
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start ()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
return;
}
if (Input.GetButton ("Shield"))
shielded = (true);
if (tag == "shieldAble" && shielded == true && other.tag == "Player") {
Destroy (gameObject);
gameController.AddScore (scoreValue /2);
Instantiate (explosion, gameObject.transform.position, gameObject.transform.rotation);
return;
} else if (other.tag == "Shot" | shielded == true)
{
Instantiate (asterExplode, other.transform.position, other.transform.rotation);
Destroy (gameObject);
Destroy (other.gameObject);
gameController.AddScore (scoreValue);
}
else
{
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
Destroy (other.gameObject);
gameController.AddScore (scoreValue);
}
}
}
2)shieldsVar - A script that was used to generate UI buttons to increase/decrease shield value. I now need to be able to call these functions inside of DestroyByContact so that they execute inside my shieldStrength script on collision
using UnityEngine;
using System.Collections;
public class shieldsVar : MonoBehaviour
{
public shieldStrength shields;
void OnGUI()
{
if (GUI.Button(new Rect(Screen.width/1.5f, Screen.height / 4 + Screen.width / 10 * 0,100,25), "Restore Shield"))
{
shields.modifyShields(1);
}
if (GUI.Button(new Rect(Screen.width/1.5f, Screen.height / 4 + Screen.width / 10 * 1,100,25), "Take Damage"))
{
shields.modifyShields(-1);
}
if (GUI.Button(new Rect(Screen.width/1.5f, Screen.height / 4 + Screen.width / 10 * 2,100,25), "Add Shield"))
{
shields.AddShields(1);
}
}
}