- Home /
public script not showing in inspector
Hi! I have a problem with this public variable which isn't showing in the inspector. I have looked at other threads and none of them helped me. If you can please help me soon.
Thanks, oshiwota
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.InputSystem;
using TMPro;
public class MovingScript : MonoBehaviour
{
public GameObject menu;
public TMP_Text total_coinsText;
public TMP_Text score;
float coins = 0;
static float total_coins = 0;
public GameObject vig;
public UnityEngine.UIElements.Slider health;
private float sec = 3;
public Sprite regularFace;
public Sprite GrossedOutFace;
public GameObject head;
[HideInInspector]
public bool isHurt;
public GameObject point;
public MyController controls; // It is this one
public GameObject bullet;
public GameObject Arm2;
Animator anim;
bool won = false;
bool Grounded;
public float size; public Vector3 offset;
Rigidbody2D RB;
public void quit()
{
Application.Quit();
}
public void Menu()
{
SceneManager.LoadScene("MenuScene");
}
public void win()
{
if (won == false)
total_coins += coins;
Time.timeScale = 0;
total_coinsText.text = "Cheese: " + total_coins.ToString();
menu.SetActive(true);
won = true;
return;
}
public void addCoin()
{
coins += 1;
}
void Hurt()
{
if (sec <= 0)
{
isHurt = false;
sec = 3;
head.GetComponent<SpriteRenderer>().sprite = regularFace;
}
else
{
health.value -= 0.0004f;
sec -= 0.005f;
head.GetComponent<SpriteRenderer>().sprite = GrossedOutFace;
}
}
void isGrounded()
{
Collider2D[] things = Physics2D.OverlapCircleAll(offset+transform.position, size);
foreach (Collider2D i in things)
{
if (i.gameObject.tag == "Ground" || i.gameObject.tag == "Enemy" || RB.velocity.y == 0)
{
Grounded = true;
}
else
{
Grounded = false;
}
}
}
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1;
anim = GetComponent<Animator>();
RB = GetComponent<Rigidbody2D>();
}
void RotateArm()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Arm2.transform.position;
difference.Normalize();
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
Arm2.transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotationZ));
if (rotationZ < -90 || rotationZ > 90)
{
if (transform.eulerAngles.y == 0)
{
Arm2.transform.localRotation = Quaternion.Euler(180, 0, -rotationZ);
}
else if(transform.eulerAngles.y == 180)
{
Arm2.transform.localRotation = Quaternion.Euler(180, 180, -rotationZ);
}
}
}
public void Shoot()
{
GameObject bul = Instantiate(bullet);
bul.transform.rotation = Arm2.transform.rotation;
bul.transform.position = point.transform.position;
}
// Update is called once per frame
void Update()
{
if (health.value <= 0)
{
SceneManager.LoadScene(1);
}
if (!won)
if (Input.GetKeyDown(KeyCode.C))
{
anim.SetTrigger("AxeTree");
}
score.text = coins.ToString() + "x";
if (Input.GetKey(KeyCode.S))
{
vig.SetActive(true);
Time.timeScale = 0.3f;
}else if (Input.GetKeyUp(KeyCode.S))
{
vig.SetActive(false);
Time.timeScale = 1;
}
if (isHurt == true)
{
Hurt();
}
controls.Player.Shoot.performed += _ => Shoot();
Vector3 lookAtTargetPos = Camera.main.WorldToScreenPoint(Input.mousePosition);
if (lookAtTargetPos.x <= 100210f)
{
transform.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
}
if (lookAtTargetPos.x > 100210f)
{
transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
}
RotateArm();
if (Input.GetAxisRaw("Horizontal") == 0)
{
anim.SetFloat("Speed", 0);
}
isGrounded();
RB.velocity = new Vector2(Input.GetAxis("Horizontal") * 5, RB.velocity.y);
if (Input.GetAxisRaw("Horizontal") == 1)
{
anim.SetFloat("Speed", 1);
transform.rotation = Quaternion.Euler(new Vector3(0, 0));
}
if (Input.GetAxisRaw("Horizontal") == -1)
{
anim.SetFloat("Speed", 1);
transform.rotation = Quaternion.Euler(new Vector3(0, 180));
}
if (Grounded)
{
if (Input.GetButton("Jump"))
{
anim.SetTrigger("IsJump");
RB.velocity = new Vector2(RB.velocity.x, 6);
}
}
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(offset+transform.position, size);
}
}
Does your $$anonymous$$yController
script have a $$anonymous$$onoBehaviour on it?
Answer by jackmw94 · Jan 12, 2021 at 04:44 PM
I expect the issue is that MyController is not serializable.
To fix this, add the [Serializiable] attribute at the top of the class like so:
[Serializable]
public class MyController
And see if that helps. If it doesn't, please post your MyController code! :)
When i change it to a class it shows errors everywhere I use it. For some reason it won't let me put all the code of the $$anonymous$$yController Script.
What is it currently? If it's a struct then keep it one :)
[Serializable]
public struct $$anonymous$$yController
If that has issues, post your $$anonymous$$yController with your reply
It keeps bringing the same errors '$$anonymous$$ovingScript.$$anonymous$$yController' does not contain a definition for 'Player' '$$anonymous$$ovingScript.$$anonymous$$yController' does not contain a definition for 'Enable' '$$anonymous$$ovingScript.$$anonymous$$yController' does not contain a definition for 'Disable'
Your answer
Follow this Question
Related Questions
Assets/SplashScreen.cs(6,14): error CS1519: Unexpected symbol `public' in cl 1 Answer
is there a way to minimize amount of public items in inspector? 1 Answer
Assign a GameObject to a Component Public Variable using c# 1 Answer
Public and SerializedField's not showing in inspector 1 Answer
I have a little problem getting a component from another script 1 Answer