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.
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;
}
}
}
public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as MonoBehaviour).enabled = false;
}