I have a script attached to my player that controls movement. I’ve been struggling to disable it via script. I tried to disable it during runtime by unchecking the box in the inspector…however, it won’t uncheck. Any idea what causes this?
P.S. - It’s not a part of being a Required Component.
Are you getting any runtime error in the console? I have no idea, personally. It hasn’t happened to me yet.
Nope. No errors whatsoever. It just won’t uncheck.
Dantus
December 13, 2014, 6:52pm
4
Are you enabling it in another script?
I don’t enable it…per-say. What I do in another script (GameMaster) is Instantiate the Player Prefab (which the script I’m trying to disable is attached to).
EDIT - But that’s handled in the OnLevelLoaded() function.
Dantus
December 13, 2014, 7:12pm
6
You need to show what exactly you are doing, otherwise, it is impossible to understand what is going on.
1 Like
Here is the script that I’m trying to disable.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float rotateSpeed = 250f;
public float moveSpeed = 5f;
private CharacterController controller;
private Transform myTransform;
private Animator anim;
void Awake(){
controller = GetComponent<CharacterController>();
myTransform = transform;
}
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
Turn();
Walk();
}
private void Turn(){
if(Mathf.Abs(Input.GetAxis("Horizontal")) > 0){
myTransform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed, 0);
anim.SetFloat("Direction", Input.GetAxis("Horizontal"));
}else{
anim.SetFloat("Direction", 0);
}
}
private void Walk(){
if(Mathf.Abs(Input.GetAxis("Vertical")) > 0){
controller.SimpleMove(myTransform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical") * moveSpeed);
anim.SetFloat("Speed", Input.GetAxis("Vertical"));
}else{
anim.SetFloat("Speed", 0);
}
}
}
In the inspector, during runtime you can click on the check box of a component on a GameObject to enable/disable it. I’m clicking the checkbox and it blinks, but it doesn’t uncheck (disable).
Dantus
December 13, 2014, 7:57pm
8
How does the code look where you disable it? Also, the enabling is important.
i added to an object and i can disable it… try adding it to an empty gameObject and disabling it? maybe something else is enabling it
I don’t think this is the problem, but here’s where the player is Instantiated.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Reflection;
using System.Xml.Serialization;
public class GameMaster : MonoBehaviour{
public Camera mainCamera;
private GameObject playerCharacter;
private GameObject headsUpDisplay;
private GameObject eventSystem;
private GameObject pc;
private PlayerCharacter pcScript;
private Vector3 playerSpawnPointPos;
void Awake(){
DontDestroyOnLoad(this);
}
void OnLevelWasLoaded() {
if(Application.loadedLevelName != "MainMenu" && Application.loadedLevelName != "CharacterGeneration"){
playerSpawnPointPos = ChooseSpawnPoint.PickSpawnPoint(Application.loadedLevelName, PlayerPrefs.GetString("Last Zone", "Town"));
playerCharacter = (GameObject)Resources.Load("Prefabs/Player Character Prefab");
pc = Instantiate(playerCharacter, playerSpawnPointPos, Quaternion.identity) as GameObject;
pc.name = "Player Character";
pcScript = pc.GetComponent<PlayerCharacter>();
Instantiate(mainCamera, Vector3.zero, Quaternion.identity);
LoadCharacterData();
headsUpDisplay = (GameObject)Resources.Load("Prefabs/HUD Prefab");
eventSystem = (GameObject)Resources.Load("Prefabs/Event System Prefab");
Instantiate(headsUpDisplay, Vector3.zero, Quaternion.identity);
Instantiate(eventSystem, Vector3.zero, Quaternion.identity);
pcScript.LastZone = Application.loadedLevelName.ToString();
SaveCharacterData();
}
}
public void SaveCharacterData(){
GameObject pc = GameObject.Find("Player Character");
PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
PlayerPrefs.SetString("Player Name", pcClass.PlayerName);
PlayerPrefs.SetInt("Player Sex", (int)pcClass.PlayerSex);
PlayerPrefs.SetInt("Player Money", pcClass.PlayerMoney);
PlayerPrefs.SetString("Last Zone", pcClass.LastZone);
}
}
Dantus
December 13, 2014, 8:29pm
11
Contrary to you, I think the disabling/enabling is exactly the problem
That code doesn’t help to understand what is going on. The enabling and disabling is definitely needed to help you further.
You can’t disable a component which doesn’t have the Update or the FixedUpdate, OnEnable…etc functions used in the script.
Dantus:
Contrary to you, I think the disabling/enabling is exactly the problem
That code doesn’t help to understand what is going on. The enabling and disabling is definitely needed to help you further.
Well I’m not (at this moment) referring to a script. I’m saying that MANUALLY (by clicking the check box) I can’t disable the script during runtime.
It does use the Update function.
1 Like
Dantus
December 13, 2014, 9:23pm
14
If you enable it by script, it makes perfect sense…
I FOUND IT!!!
It’s my HUD GameObject. It has a script attached to it. And in that script, it states that if the Menu (a child GameObject of the HUD) is active, to disable the Movement on the player, but if the Menu is NOT active, to enable the Movement on the player. So the problem is that as long as the Menu is NOT active, if I try to disable the Movement in any other way, the script on the HUD is just going to re-enable it right away.