- Home /
Error wth enabling disabling scripts.
Alright this script has been put on an empty game object which contains my player. THe player has two scripts (different weapons/guns.etc) and with the key input of different keys I want it to enable and disable script. Probably another nib question.
using UnityEngine; using System.Collections;
public class WeaponChoose : MonoBehaviour {
private bool pistolequiped = true;
private bool shotgunequiped = false;
// Update is called once per frame
void Update () {
if ( Input.GetKeyDown ("1"))
{
if (pistolequiped == false)
{
GetComponent("PistolScript").enabled = true;
GetComponent("ShotgunScript").enabled = false;
}
if (pistolequiped == true)
{
// sounds etc,
}
}
if ( Input.GetKeyDown ("2")) { if (shotgunequiped == false) { GetComponent("PistolScript").enabled = false; GetComponent("ShotgunScript").enabled = true; } if (shotgunequiped == true) { // sounds etc } } } }
I get the error: Assets/WeaponChoose.cs(26,34): error CS1061: Type UnityEngine.Component' does not contain a definition for
enabled' and no extension method enabled' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Have I mixed up my Java with C# during the scripting or am I just doing something wrong. Cheers, (Im off to sleep now will check on answer in the morning)
Answer by efge · Dec 12, 2010 at 11:22 AM
Javascript:
private var Pistol : PistolScript;
function Start () {
Pistol = GetComponent(PistolScript);
Pistol.enabled = false;
}
function Update () {
if ... Pistol.enabled = true; ... }
Here is the Reference for GetComponent.
PS: Good Morning!
Thanks for that, I converted it into C# and got it working the way I wanted but later on I decided I could do it a better way. Thanks anyway, I will know what to do next time :D.
Unsure who keeps negativing (not even a word <<) my questions though ;(
Your answer
Follow this Question
Related Questions
just a simple question.. 1 Answer
How to make movement on camera on button down. 1 Answer
Help Changing Materials On Button Click 0 Answers