- Home /
Can't make script work. & Script help.
Hello, I'm making an Fps game and I'm making some scripts to shoot and an enemy script. My problem is that in my shooting script I can't access the variable inside the enemy script. My other problem is that my enemy script makes the guy take damage, but in order to make both scripts work that I know of is to make a seperate function or void different than the update that way I can access the enemy damage but the way the script is set up the enemy will take constant damage because I need a bool to check if the enemy is hit but I'm not sure how to set it up. I hope I'm making sense. All I need is how to make my enemy know if it's hit, and set up the function to make it hurt, and set up my gun script to make it access the function to make the guy get hurt. Heres my scripts. First my enemy, then my gun;
using UnityEngine;
using System.Collections;
public class AI : MonoBehaviour {
public float suspition;
public int health = 100;
public Transform target;
public bool foundTarget = false;
public int moveSpeed;
public int rotateSpeed;
private bool takeDamage = false;
public Transform myTransform;
public float minDist = 2.0f;
public float maxDist = 10.0f;
// Use this for initialization
void Start () {
takeDamage = false;
myTransform = transform;
target = GameObject.FindWithTag("Player").transform;
suspition = 0.0f;
}
// Update is called once per frame
void Update () {
if(suspition == 100)
{
foundTarget = true;
}
else{
foundTarget = false;
}
if(foundTarget && health > 0)
{
transform.LookAt(target);
if(Vector3.Distance(transform.position, target.position) >= minDist)
{
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
if(Vector3.Distance(transform.position, target.position) <= maxDist)
{
//gameObject.GetComponent(AIShoot).Shot();
}
}
}
public void TakeDamage()
{
if(health == 0)
{
gameObject.SetActive(false);
takeDamage = false;
}
health -= 10;
}
}
My Gun;
using UnityEngine;
using System.Collections;
public class RaycastShoot : MonoBehaviour {
public Transform spawnPoint ;
public int speed;
public float nextFire;
public float fireRate;
public int ammo;
public GUIText ammoText;
public AudioSource shotSound;
public Transform wood;
public Transform sand;
public Transform dirt;
public Transform water;
public Transform enemy;
public float range = 100.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire)
{
if(ammo > 0)
{
Shot();
audio.Play();
ammo --;
}
}
}
void Shot()
{
nextFire = Time.time + fireRate;
RaycastHit hit;
Vector3 fwd = spawnPoint.TransformDirection(Vector3.forward);
Debug.DrawRay(spawnPoint.position, fwd);
if(Physics.Raycast(spawnPoint.position, fwd, out hit, range)) {
if(hit.collider.tag == "Dirt") {
Instantiate(dirt, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if(hit.collider.tag == "Sand") {
Instantiate(sand, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if(hit.collider.tag == "Wood") {
Instantiate(wood, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
}
if(hit.collider.tag == "Water") {
Instantiate(water, hit.point, Quaternion.identity);
}
if(hit.collider.tag == "Enemy") {
Instantiate(enemy, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
enemy = gameObject.GetComponent(AI).GetComponent(TakeDamage());
}
}
}
void OnGUI() {
ammoText.text = "Ammo: " + ammo.ToString();
}
}
Answer by getyour411 · Jan 25, 2014 at 06:43 AM
This looks like a pretty ordinary question in terms of: Script A needs a variable from Script B.
Please take a few minutes to learn how to do this: http://unitygems.com/script-interaction1/
The initial question was pretty hard to follow; if you still have an issue after reviewing the GetComponents reivew on Unity Gems, post a follow-up with a little more clarity.
I ended up figuring out the problem, so can you please delete this answer that way I can delete the post?
Your answer
Follow this Question
Related Questions
Accessing function from another script won't work 1 Answer
Accessing a function through its name stored in a string JS 2 Answers
Please someone help with this please been trying to get this working for 4 days 1 Answer
Problem with raycast shooting 1 Answer
Call a Function from another Script 1 Answer