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 XaXa123 · May 30, 2013 at 09:04 PM · c#combinetwo

Combine two scripts

Why do when I combine two scripts my Crosshair shows but dont moves. Where is my error ? Who can corretly convert this two scrips ? I mean if click Fire button during reloading the crosshair will move. And Crosshair must move depending on shooting and fire rate. There is the first script:

 using UnityEngine;
 
 using System.Collections;
 
  
 
 public class Weapon : MonoBehaviour {
 
                 //Camera
 
                 public Transform _camTransform;
 
  
 
                 //Animation
 
                 public AnimationClip animationIdle;
 
                 public AnimationClip animationRun;
 
                 public AnimationClip animationFire;
 
                 public AnimationClip animationReload;
 
  
 
                 //Sounds
 
                 public AudioClip soundReload;
 
                 public AudioClip soundFire;
 
  
 
  
 
                 public float ReloadTime = 3.5f;
 
                 public float FireTime = 0.5f;
 
                 public float FireRate = 0.5f;
 
                 public float Range = 1000;
 
                 public int Damage = 50;
 
                 public int BulletsInClip = 8;
 
                 public int Clips = 3;
 
  
 
  
 
                 public ParticleEmitter muzzleFlash;
 
  
 
  
                 private int BulletsLeft = 0;
 
                 private float NextFireTime = 0.0f;
 
                 private bool Reloading = false;
 
                 private bool isFire = false;
 
                 private float muzzleFlashLifeTime = 0.0f;
     
 
  
 
  
 
                 void Start () {
 
                                BulletsLeft = BulletsInClip;
 
                 }
 
  
 
  
 
                 void Update () {
 
  
 
                                if (muzzleFlashLifeTime > 0) {
 
                                                muzzleFlashLifeTime--;
 
                                                muzzleFlash.emit = true;
 
  
 
                                } else {
 
                                                muzzleFlash.emit = false;
 
                                                                               }
 
                                if (BulletsLeft == 0 && !Reloading && NextFireTime < Time.time) {
 
                                                Reload ();
 
                                }
 
  
 
                                if (Input.GetButtonDown("Fire1")) {
 
                                                Fire();
 
                                } else if (Input.GetKeyDown(KeyCode.R) ) {
 
                                                Reload ();
 
  
 
                                } else if( !Reloading && !isFire ) {
 
                                                Idle ();
 
                                }
 
  
 
                 }
 
  
 
                 private void Reload() {
 
                                if (Clips == 0) return;
 
                                Reloading = true;
 
                                Invoke ("ReloadingOf", ReloadTime);
 
                                animation.Stop( animationRun.name );
 
                                animation.CrossFade( animationReload.name );
 
                                audio.PlayOneShot( soundReload );
 
  
 
                 }
 
  
 
                 private void Idle() {
 
                                if(Input.GetKey( KeyCode.W)) {
 
                                                animation.CrossFade( animationRun.name );
 
                                } else {
 
                                                animation.CrossFade( animationIdle.name );
 
                                }
 
  
 
                 }
 
  
 
                 private void Run() {
 
                                animation.CrossFade( animationRun.name );
 
                 }
 
  
 
                 private void ReloadingOf() {
 
                                BulletsLeft = BulletsInClip;
 
                                Clips--;
 
                                Reloading = false;
 
                 }
 
  
 
                 private void FireOf() {
 
                                isFire = false;
                 }
 
  
 
                 private void Fire() {
 
                                isFire = true;
 
                                Invoke ("FireOf", FireTime);
 
                                if (BulletsLeft == 0)
 
                                                return;
 
  
 
  
 
                                if (Time.time - FireRate > NextFireTime)
 
                                                NextFireTime = Time.time - Time.deltaTime;
 
  
 
                                if (NextFireTime < Time.time && BulletsLeft != 0 && !Reloading) {
 
             
                                                
  
 
                                                animation.Rewind ( animationFire.name );
 
                                                animation.CrossFade( animationFire.name );
 
                                                audio.PlayOneShot( soundFire );
             

                                               {        
 //                                              Debug.Log("fire!");
                                                StaticFunction.GenerateDecal(rayhits);
                                               } 
 
                                                Effects();
 
  
                                                NextFireTime += FireRate;
 
                                                BulletsLeft--;
 
  
 
                                                Vector3 _direction = _camTransform.TransformDirection(Vector3.forward);
 
             /*RaycastHit _hit;
 
             if (Physics.Raycast(_camTransform.position, _direction, out _hit, Range))
 
             {
 
                 if (_hit.transform.tag == "Enemy")
 
                 {
 
                     BotHealth eh = _hit.transform.GetComponent<BotHealth>();
 
                     eh.adjustHealth(-Damage, transform);
 
                 }
 
             }*/
 
                     
                                }
 
  
 
                 }
 
  
 
                 private void Effects () {
 
                                muzzleFlashLifeTime = 2.0f;
 
  
 
                 }
 
  
 
                 void OnGUI() {
 
                                int lHeight = 25;
 
  
 
                                int lWidth = 200;
 
  
 
                                Rect _pos = new Rect(Screen.width - lWidth, Screen.height - 50, lWidth, lHeight);
 
                                Rect _pos2 = new Rect(Screen.width - lWidth, Screen.height - 30, lWidth, lHeight);
 
                                string lb = "Bullets: " + BulletsLeft;
 
                                GUI.Label(_pos, lb);
 
                                string lb2 = "Clips: " + Clips;
 
                                GUI.Label(_pos2, lb2);
                                
 
  
 
                 }
 
  
 
 }

There is the crosshair script:

 using UnityEngine;
 using System.Collections;
 [ExecuteInEditMode]
 public class Proba : MonoBehaviour {
     //public preset crosshairPreset = preset.none;
     public bool  showCrosshair = true;
     public Texture2D verticalTexture;
     public Texture2D horizontalTexture;
     public float cLength = 10;
     public float cWidth = 3;
     //Spreed setup
     public float minSpread = 45.0f;
     public float maxSpread = 250.0f;
     public float spreadPerSecond = 150.0f;
     
     
     private Texture temp;
     private float spread;
     public GUIStyle verticalT;
     public GUIStyle horizontalT;
     
     // Update is called once per frame
     void Update () {
                 //Used just for test (weapon script should change spread).
         if(Input.GetButton("Fire1")) spread += spreadPerSecond * Time.deltaTime; 
         else spread -= spreadPerSecond * 2 * Time.deltaTime;   
     
     }
     void OnGUI(){
         if(showCrosshair && verticalTexture && horizontalTexture){
                 //var verticalT = GUIStyle();
                 //var horizontalT = GUIStyle();
                 verticalT.normal.background = verticalTexture;
                 horizontalT.normal.background = horizontalTexture;
                 spread = Mathf.Clamp(spread, minSpread, maxSpread);
                 //Vector2 pivot = new Vector2(Screen.width/2, Screen.height/2);
                 
                 //if(crosshairPreset == preset.none)
                {
                         
                         //Horizontal
                         GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height - spread)/2 - cLength, cWidth, cLength), temp, horizontalT);
                         GUI.Box( new Rect((Screen.width - cWidth)/2, (Screen.height + spread)/2, cWidth, cLength), temp, horizontalT);
                         //Vertical
                         GUI.Box( new Rect((Screen.width - spread)/2 - cLength, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                         GUI.Box( new Rect((Screen.width + spread)/2, (Screen.height - cWidth)/2, cLength, cWidth), temp, verticalT);
                 }
         }
 }
 }


 
Comment
Add comment · Show 5
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 jerichaosymphony · May 30, 2013 at 09:18 PM 0
Share

start with smaller scripts? What does the 1st script do,how do you want to combine them?

avatar image XaXa123 · May 30, 2013 at 10:19 PM 0
Share

The first script is gun script. There is animation, sounds. Too that shows bullets in clips, how much clips is left.

avatar image XaXa123 · May 30, 2013 at 10:20 PM 0
Share

If click Fire button during reloading the crosshair will move. And Crosshair must move depending on shooting and fire rate.

avatar image Memige · May 30, 2013 at 10:22 PM 0
Share

Both of these scripts work correctly when used separately, correct?

avatar image XaXa123 · May 30, 2013 at 10:39 PM 0
Share

Yes. I think that I'm not correctly combine that scripts.

0 Replies

· Add your reply
  • Sort: 

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unfixable Assets/Scripts/PlayerAttack.cs(30,15): error CS1525: Unexpected symbol `private' 1 Answer

GC collects variable that is still in use 1 Answer

how to make one script change a variable in another scipt 2 Answers


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