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 Condore · Oct 20, 2012 at 12:08 AM · animatorcharacterswitchcombo

Switching between scripts within the inspector using code

Hey guys, me again,

So I'm having a major problem within our game, right now I'm trying to switch between a script that is attached to two different gameobjects when I push RB on my Xbox 360 controller.

What I'm trying to do within my game is have it, so that when I push the button our character swaps out their current, assigned "character" script and the variable I have setup, called "current armor" changes to the different objects attached, animator script. This comes with different attack animations specific to the armour that you've switched to.

The problem I'm having is that, even though I've declared it to switch between the animator scripts within code... it's not doing it for some reason when I run the game and hit the button. I'm just wondering if I have to declare it differently or if I've messed my code up somehow?

Here's the animator code, as well as the switching armor code. If you can see anything wrong with either of them, please let me know because, in theory my code should be working... :S

Switching Armour Script

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SwitchingArmor : MonoBehaviour
 {
 
 //Armor sets arrays we want to change
     public GameObject[] darkArmor;
     public GameObject[] lifeArmor;
     Combo zombocom;
     bool lifeArmorvisible = false;
     
 
 // Update is called once per frame
     void Start ()
     {
         foreach (GameObject lifePiece in lifeArmor) 
         {
             lifePiece.renderer.enabled = false;    
         }
  
         zombocom = GetComponent<Combo>();
     }
 
     void Update ()
     {
         if (Input.GetButtonDown ("Switch")) 
         {
             lifeArmorvisible = !lifeArmorvisible;
  
             foreach (GameObject lifePiece in lifeArmor) 
             {
                 lifePiece.renderer.enabled = lifeArmorvisible;
                 zombocom.currentArmor = zombocom.whiteArmor;
                 zombocom.whiteArmor.GetComponent<TP_Animator>();
             }
  
  
             foreach (GameObject darkPiece in darkArmor) 
             {
                 darkPiece.renderer.enabled = !lifeArmorvisible;
                 zombocom.currentArmor = zombocom.blackArmor;
                 zombocom.blackArmor.GetComponent<TP_Animator>();
             }
  
         }
     }
 }

Combo Script

 using UnityEngine;
 using System.Collections;
 
 public class Combo : MonoBehaviour
 {
     public float attack1Time = 1.2f;
     public float attack2Time = 1.2f;
     public float attack3Time = 1.2f;
     float minTimer = 0.6f;
     float maxTimer = 0.0f;
     public float buffer = 0.57f;
     
     public TP_Animator character;
 
     public GameObject currentArmor;
     
     public GameObject whiteArmor;
     public GameObject blackArmor;
     
     public katCollision katCollision;
     public Collision_clay clayCollision;
         
     int fired = 0;
     
     // Use this for initialization
     void Start ()
     {
         katCollision = GetComponentInChildren<katCollision>();
         clayCollision = GetComponentInChildren<Collision_clay>();
         
         character = blackArmor.GetComponent<TP_Animator>();
         currentArmor = blackArmor;
     }
     
     // Update is called once per frame
     void Update ()
     {
         minTimer -= Time.deltaTime;
 
         maxTimer -= Time.deltaTime;
 
         //reset if player hasn't hit button in time
         if (fired >= 1) 
          {
             
             if (maxTimer < 0) 
             {
                 fired = 0;
             }
         }
         
         if (Input.GetButtonDown ("Fire3")) 
         {
             
             if(minTimer <= 0)
             {
                 
                 switch (fired) {
         
                 
                     case 0:
                         {
                             minTimer = attack1Time - buffer;
                             Debug.Log ("min timer " + minTimer);
                             maxTimer = attack1Time + buffer;
                             Debug.Log ("max timer" + maxTimer);
                                 
                             fired = 1;
 //                            Debug.Log("attack 1");
                             
                             if(currentArmor == blackArmor)
                             {
                                 katCollision.comboNumber = fired;
                                 character.Attack();
                                 Debug.Log ("death armor attack 1");
                             }
                             else
                             {
                                 character.Idle();
                             }
                             
                             if(currentArmor == whiteArmor)
                             {
                                 clayCollision.comboNumber = fired;
                                 character.AttackClaymore();
                                 Debug.Log ("light armor attack 1");
                                 
                             }
                             else
                             {
                                 character.Idle();
                             }
                             
                                 
                             break;
                         }
                     case 1:
                         {
                             minTimer = attack2Time - buffer;
                             Debug.Log ("min timer " + minTimer);
                             maxTimer = attack2Time + buffer;
                             Debug.Log ("max timer" + maxTimer);
                             
                             fired = 2;
 //                            Debug.Log("attack 2");
                             
                             if(currentArmor == blackArmor)
                             {
                                 katCollision.comboNumber = fired;
                                 character.Attack1();
                                 Debug.Log ("death armor attack 2");
                             }
                             else
                             {
                                 character.Idle();
                             }
                             
                             if(currentArmor == whiteArmor)
                             {
                                 clayCollision.comboNumber = fired;
                                 character.AttackClaymore1();
                                 Debug.Log ("light armor attack 2");
                             }
                             else
                             {
                                 character.Idle();
                             }
                         
                             
                         
                             break;
                         }
                     case 2:
                         {
                             minTimer = attack3Time - buffer;
                             Debug.Log ("min timer " + minTimer);
                             maxTimer = attack3Time + buffer;
                             Debug.Log ("max timer" + maxTimer);
                             
                             fired = 3;
 //                            Debug.Log ("attack 3");
                         
                             if(currentArmor == blackArmor)
                             {
                                 katCollision.comboNumber = fired;
                                 character.Attack2();
                                 Debug.Log ("death armor attack 3");
                             }
                             else
                             {
                                 character.Idle();
                             }
                             
                             if(currentArmor == whiteArmor)
                             {
                                 clayCollision.comboNumber = fired;
                                 character.AttackClaymore2();
                                 Debug.Log ("light armor attack 3");
                             }
                             else
                             {
                                 character.Idle();
                             }
                         
                                 
                                 break;
                 
                         }
                     }
                 }
             }
         }
 }
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
Best Answer

Answer by MountDoomTeam · Oct 20, 2012 at 04:56 AM

that's a lot of text for a small question! Have both scripts enabled on the game objects and to switch them do- otherwise you could also addcomponent, and destroy component i use this : GetComponent("myScript").enabled = true;//(false to disable) the "myScript" is attached to the same gameObject.

works great

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 Condore · Nov 09, 2012 at 11:47 PM 0
Share

Thanks so much! This fixed the problem. ;D

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

10 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

Related Questions

2D Character Switch Systsem (Same Location) 1 Answer

Performance Issue When Applying animator.rootRotation 0 Answers

Player switch on ui button 0 Answers

How would I switch between 2 animation states connected to 1 state under input? 0 Answers

How to fix broken combo animation? 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