- Home /
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;
}
}
}
}
}
}
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