• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by oshiwota · Jan 12 at 01:51 PM · public variable

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);
     }
 
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jan 12 at 02:43 PM 0
Share

Does your MyController script have a MonoBehaviour on it?

avatar image oshiwota Llama_w_2Ls · Jan 12 at 04:13 PM 0
Share

No it doesn't

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jackmw94 · Jan 12 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! :)

Comment
Add comment · Show 23 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oshiwota · Jan 12 at 04:45 PM 0
Share

ok i'll try that. Thanks for the feedback!

avatar image oshiwota · Jan 12 at 04:55 PM 0
Share

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 MyController Script.

avatar image jackmw94 oshiwota · Jan 12 at 05:00 PM 0
Share

What is it currently? If it's a struct then keep it one :)

  [Serializable]
  public struct MyController



If that has issues, post your MyController with your reply

avatar image oshiwota jackmw94 · Jan 12 at 05:05 PM 0
Share

It keeps bringing the same errors 'MovingScript.MyController' does not contain a definition for 'Player' 'MovingScript.MyController' does not contain a definition for 'Enable' 'MovingScript.MyController' does not contain a definition for 'Disable'

Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

cashing in Scenes to a public vaiable 1 Answer

Can't assign variable to prefab, or apply changes to prefab. 1 Answer

Manipulating a 2nd game object's position using OnMouseDown on another Game Object? 0 Answers

C# how do I make a public variable dropdown? 1 Answer

Prefab is not shown in unity 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges