Disable a Component with a script. Not working
I tried to disable the Script "FirstPersonController" on the FPSController, "MeleeSystem" on FirstPersonCharacter and "MeshRenderer" on Axe. Here you can see the hierarchy:
Here you can see my Code:
public class RespawnSystem : MonoBehaviour
{
Component freezePlayer;
Component freezeAttacking;
Component weapon;
public Transform respawnLocation;
public static bool playerIsDead = false;
public Button buttonRespawn;
public Button buttonMainMenu;
public Button buttonQuitGame;
void Start()
{
freezePlayer = GetComponent("FirstPersonController");
freezeAttacking = GameObject.Find("FirstPersonCharacter").GetComponent("MeleeSystem");
weapon = GameObject.Find("Axe").GetComponent("MeshRenderer");
}
void Update()
{
if (playerIsDead == true)
{
// freezePlayer.enabled = false
// freezeAttacking.enabled = false
// weapon.enabled = false;
buttonRespawn.gameObject.SetActive(true);
buttonMainMenu.gameObject.SetActive(true);
buttonQuitGame.gameObject.SetActive(true);
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
}
The Lines with the Comment are not working.
Error: Error 2 "UnityEngine.Component" does not contain a definition for "enabled" and could not find an extension method "enabled" that accepts a first UnityEngine.Component argument.
Thank you for the help! Nicolai
I am really sorry for my bad English!
Answer by Cepheid · Nov 18, 2016 at 04:48 PM
When you are trying to cache components as variables it's better to use the actual component class as the datatype rather than using the base class Component. This is because the component base class does not have a variable for enabling and disabling components.
So, if you want to get the FirstPersonController component, then specify the FirstPersonController as the datatype. For example, you would change your variables to the following:
public class RespawnSystem : MonoBehaviour
{
FirstPersonController freezePlayer;
MeleeSystem freezeAttacking;
MeshRenderer weapon;
// ...
}
Also, when getting components, it's often better to use the generic way. So, all together, your class should now look like:
public class RespawnSystem : MonoBehaviour
{
FirstPersonController freezePlayer;
MeleeSystem freezeAttacking;
MeshRenderer weapon;
public Transform respawnLocation;
public static bool playerIsDead = false;
public Button buttonRespawn;
public Button buttonMainMenu;
public Button buttonQuitGame;
void Start()
{
freezePlayer = GetComponent<FirstPersonController> ();
freezeAttacking = GameObject.Find("FirstPersonCharacter").GetComponent<MeleeSystem> ();
weapon = GameObject.Find("Axe").GetComponent<MeshRenderer> ();
}
void Update()
{
if (playerIsDead == true)
{
freezePlayer.enabled = false
freezeAttacking.enabled = false
weapon.enabled = false;
buttonRespawn.gameObject.SetActive(true);
buttonMainMenu.gameObject.SetActive(true);
buttonQuitGame.gameObject.SetActive(true);
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
}
}
I get an Error on line 3 and 15. The Error said: The type or namespacename "FirstPersonController" could not be found. $$anonymous$$eleeSystem and weapon is working though.
Ah yes, that's because Unity places it's FirstPersonController under a seperate namespace, I forgot about that. Simply add the following line to the top of the file:
using UnityStandardAssets.Characters.FirstPerson
That should fix it.
Answer by kami1339 · Mar 28, 2019 at 01:02 PM
use this
public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
}