The script is missing a checkbox - c#

I was programming a script and when I want to add it to GameObject, I notice that the enable check disappeared and can't to drag to scripts to the inspector for this component Event Script.
I tried change the names of the scripts but there aren't changes.
The script to add:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EventScript : MonoBehaviour
{
private GameObject obj;
public SaveObjScript saveObjScript;
public SpawnScript spawnScript;
public void InitObject(){
Debug.Log("OBJETO A ACCIONAR ES: " + saveObjScript.getObject().name);
//obj = saveObjScript.getObject();
}
public void ChangeColor(){
InitObject();
Renderer rend = obj.GetComponent<Renderer>();
rend.material.color = Random.ColorHSV();
}
public void RemoveObj(){
InitObject();
Destroy(obj);
spawnScript.mTotalObjetos --;
}
}

It has no checkbox because it doesn't implement any built-in methods like Start or Update or FixedUpdate that care about whether the component is enabled.
From the MonoBehaviour documentation:
Note: There is a checkbox for disabling MonoBehaviour on the Unity Editor. It disables functions when unticked. If none of these functions are present in the script, the Editor does not display the checkbox.

Related

Unity : Can't drag GameObject with Text on Script reference

I'm trying to change the Text of an UI GameObject with a script but Unity doesn't let me drag the GameObject with the text on the script reference and when i do public GameObject it says GameObject is not a Text.
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEngine.UI;
public class GhostName : MonoBehaviour
{
public Text NameText;
void Start()
{
NameText.text = "The Name is : ";
}
}
You need to store NameText as a reference to a GameObject, and then call GetComponent<Text>() to get the Text component on the object. This way, you'll be able to drag the game object into the 'NameText' field in the Unity Inspector window.
...
public class GhostName : MonoBehaviour
{
public GameObject NameText;
void Start()
{
NameText.GetComponent<Text>().text = "The Name is : ";
}
}
You should also check that GetComponent<Text>() is not equal to null before setting the value of .text.
If anyone still get this issue, I had to change from "TextMeshPro" to "Text" Object.
Disclaimer: I am totally new to Unity. But it seems I have the same issue.
Under Hierarchy -> right click -> UI, there is only "Text - TextMeshPro" option in my Unity version (2021.3.4f1). I don't know why "Text" is missing. (anyone concur with me?)
So I fail to follow this step in a beginner tutorial, I anyway just select TextMeshPro instead. Then I just cannot drag to script. I guess it is because the tutorial is using Text type not TextMeshPro and they are not compatible.
Later I realize that, to enable the drag action, one cannot use [SerializeField] Text xxx; but must use [SerializeField] TextMeshProUGUI xxx;.
Pls also include using TMPro; in the header.
I used TMPro namespace and public TextMeshProUGUI scoreText;
and I was able to drag and drop the Text (TMP) to the script as below:
using TMPro;
using UnityEngine;
public class className : MonoBehaviour
{
// Start is called before the first frame update
public TextMeshProUGUI scoreText;
void Start()
{
scoreText.text = "Score: "+ points;
}

Trying to activate an animation trigger in UNITY by a controler button press, shows multiple errors on unity, but not on compiler

I am trying to make an animation switch using a trigger parameter on UNITY, but even though the debugger doesn't show any bugs, unity shows me a list of errors and doesn't let me run the program.
The list of errors:
Here is the list of errors shown
How I configured the animator:
animator
The input:button actions
The code in C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class SVITCH : MonoBehaviour
{
public Animator Anim;
public PlayerControls controls;
void Awake()
{
Anim = GetComponent<Animator>();
controls = new PlayerControls();
controls.Control.IdleSwitch.performed += ctx => Switcher();
}
void Switcher()
{
Anim.SetTrigger("sitch");
}
void OnEnable()
{
controls.Control.Enable();
}
void OnDisable()
{
controls.Control.Disable();
}
}
new errors:new Errors
switch is a keyword, therefore cannot be a class name. C# contains reserved words that have a special meaning for the compiler. These reserved words are called keywords. Keywords cannot be used as an identifier (name of a variable, class, interface, etc.).
Try changing it to Switch.

TextMesh Pro text will not change via script

I cannot seem to change my TextMeshPro value via script.
In my inspector I have a TextmeshPro object named Countdown. I have a script named GameController which is attached to this.
My script then sets the string value of Countdown to Hello but it does not work.
GameController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GameController : MonoBehaviour {
public TextMeshProUGUI Countdown;
// Use this for initialization
void Start () {
Countdown = GetComponent<TextMeshProUGUI> ();
Countdown.text = "Hello";
}
// Update is called once per frame
void Update () {
}
}
In the inspector there is a field for TextMesh but I cannot drag the CountDown object to this for some reason, could that be the issue?
the problem is that you are using a regular TextMeshPro object, and in your code your looking for a TextMeshProUGUI, simple mistake. change code to:
public class GameController : MonoBehaviour {
public TextMeshPro Countdown;
// Use this for initialization
void Start () {
//you shouldnt need to get component the editor should take care of this for you when
//you drop it since you have the object set to TextMeshPro and not just GameObject
Countdown = GetComponent<TextMeshPro> ();
Countdown.text = "Hello";
}
// Update is called once per frame
void Update () {
}
}
the only way to make a TextMeshProUGUI object is to add it through a canvas. in your scene when you just add a TMP it will be Regular TMP which your "countdown" is. you can tell because it uses the TMP script not the TMPUGUI script.

Why in Editor type script it's never get to the break point?

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class GetPrefabs : Editor
{
public GameObject[] prefabs;
// Start is called before the first frame update
void Start()
{
prefabs = (GameObject[])Resources.LoadAll("Assets/Test/Animations/");
}
// Update is called once per frame
void Update()
{
prefabs = (GameObject[])Resources.LoadAll("Assets/Test/Animations/");
}
}
First I tried to put the script inside Assets/Test/Editor but it didn't work then I moved the script to Assets/Editor but it's not working either it's never get to the break point I put on the line in the Update or in the Start.
GetPrefabs derives from Editor. The Start() and Update() are MonoBehaviour methods (i.e. Unity looks for them if you derive from MonoBehaviour). You should look at Unity docs for the Editor class and pick appropriate methods from its list - https://docs.unity3d.com/ScriptReference/Editor.html

In unity C# script why Input is not exist?

using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (
}
}
Inside the if ( i type Input but it's not exist.
I tried to add in the top using.System.Io; but that's not the solution.
In my unity project i clicked in the menu on Assests > open C# Project and it opened a new mono develop scripting window.
In my unity project i have a firstpersoncharacter and under in it a spotlight.
I want to make in the script a key trigger like if i click on the key F it will turn on the spotlight and if i click on F again it will turn the spotlight off.
if there is no input you should check script:
1.fix compiler errors
2.if you haven't libraries you can not access to input or etc?
using UnityEngine;
using System.Collections;
3.if you can't access to input you should install new monodevelop version from monodevelop.com
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public bool mybool;
void Update() {
if (Input.GetKeyDown(KeyCode.F))
GetComponent<Light> ().enabled = !mybool;
}
}
explain:
!false = true
!true = false

Categories