Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FPSworrior · Jan 25, 2014 at 05:21 AM · functionaccess

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();
     }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FPSworrior · Jan 25, 2014 at 05:36 PM 0
Share

I ended up figuring out the problem, so can you please delete this answer that way I can delete the post?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges