Why static variables do not appear in Inspector in Unity - unity3d

I have Example.cs which has attached to the Main Camera, and in the Main Camera's Inspector I just can see the aPoint appear. Both aPoint and bPoint are public but why static makes bPoint doesn't appear in the Inspector.
using System.Collections;
using UnityEngine;
public class Example : MonoBehavior
{
public int aPoint;
public static int bPoint;
}

Related

Edit array elements properties in editor

So...
I was watching some old unity tutorial videos and saw people editing properties of the elements of an array in editor like this:
When I tried in my project the editor does not showed the properties, just show a place to select an already created element:
Why the "Clip", "Volume" and "Name" properties are missing when I do it in my project?
My classes:
using UnityEngine;
public class AudioManager : MonoBehaviour
{
[SerializeField]
public Sound[] Sounds;
}
[System.Serializable]
public class Sound : MonoBehaviour
{
public AudioClip Clip;
public string Name;
[Range(0f, 1f)]
public float Volume;
[Range(.1f, 3f)]
public float Pitch;
}
I'm using Unity 2021.3.5f1
The problem is that your Sound class inherits from Object (MonoBehaviour), and Object are always serialized as reference. Make your Sound not inherit MonoBehaviour and it should look like in example (except that you have newer version of unity with reorderable list drawer)

(Unity) I cannot drag my UI components in my GameManager

UI components and GameManager
I'm following a video tutorial on Unity and the person just dragged those items in the GameManager slots, what I am not able to do.
Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public GameObject ballPrefab;
public GameObject PlayerPrefab;
public Text scoreText;
public Text ballsText;
public Text levelText;
void Start()
{
}
void Update()
{
}
}
The problem is that you selected the GameManager inside your project folder and not inside your hierachy. Thats why you can drag & drop Prefabs inside of it but not your Text inside your hierachy.

Unity URP 2D light

I've recently discovered the URP and am developing a 2D game for fun. I added a global 2d light and set up some code to grab the light component, but unity keeps saying that light 2D does not exist. The error in unity is "error CS0246: The type or namespace name 'Light2D' could not be found". This is the code I have so far. I also followed a tutorial by Jimmy Vegas to grab the system time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class DayNight : MonoBehaviour
{
public GameObject theDisplay;
public GameObject GL;
public int hour;
public int minutes;
public int totaltime;
private bool AM;
void Start()
{
totaltime = System.DateTime.Now.Hour;
hour = System.DateTime.Now.Hour;
minutes = System.DateTime.Now.Minute;
}
public void Night()
{
//change light settings
GL.GetComponent<Light2D>().Intensity = 0.4;
}
}
To reference the Light2D Component you need to add the UnityEngine.Experimental.Rendering.Universal namespace to your code.
Namespace Example:
...
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
public class DayNight : MonoBehaviour
{
...
}
If you get an Error you need to just continue and try to compile it, to see if it actually works.
After that is done you should be able to access the Light2D Component of your GameObject without any further troubles.
Calling Light2D Example:
public float nightvalue = 0.4f;
public void Night()
{
// Change light intensity to nightvalue.
GL.GetComponent<Light2D>().intensity = nightvalue;
}

Unity scriptable object only serializing the unity objects

I am trying to make a spell system. My base class for the system looks like this:
public abstract class MagicBook : ScriptableObject {
public Sprite bookSprite;
public float manaCost;
public float coolDown;
public bool rapidFire;
[HideInInspector] public bool coolDownElapsed;
public virtual void OnActivate() {
}
public virtual void OnActivate(Vector3 position, Quaternion rotation) {
}
}
Then I have another class extending from MagicBook where I override the OnActivate function. My problem now is that in the inspector only the variable bookSprite is showing and all the other values are not there. I tried adding an [SerializeField] in front of the variables and define them new in the extending class. But they still dont show. Has anyone an idea why they are not showing or how I can fix this?
Thanks for your help and time.
Did you create Asset for ScriptableObject?
Add attribute to extended class:
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/NewBook", order = 1)]
And then create asset with
Another idea:
Probably you have custom drawer for this object, but...
Turn your inspector into debug mode and check if properties are visible there.
Cannot reproduce this issue with Unity 2020.1.0f1.
public class NewBook : MagicBook
{
}

XNA Game class Error

I have project, inside there is background.cs class and game1.cs class,
thats my background.cs class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Rocket
{
class Backgrounds
{
public Texture2D texture;
public Rectangle rectangle;
public void Draw(SpriteBatch spriteBatch){
spriteBatch.Draw(texture, rectangle, Color.White);
}
class Scrolling : Backgrounds{
public Scrolling(Texture2D newTexture, Rectangle newRectangle){
texture = newTexture;
rectangle = newRectangle;
}
public void Update(){
rectangle.X -= 3;
}
}
}
}
and thats game1.cs class code (starting where I get error):
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Rocket
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Scrolling scrolling1;
Scrolling scrolling2;
'
So, Scrolling scrolling1; is underlined, (as second one), it says that class Scrolling could not be found, but it exists! I am noob in XNA and I cant find why it isn't working. any help will be ok!
Why do you have your Scrolling class nested into Backgrounds class in background.cs? The default visibility for nested classes in C# is private and therefore it is not visible to the Game1 class.
You should put this
class Scrolling : Backgrounds{
public Scrolling(Texture2D newTexture, Rectangle newRectangle){
texture = newTexture;
rectangle = newRectangle;
}
public void Update(){
rectangle.X -= 3;
}
}
into a file scrolling.cs. This gives Scrolling and Background the default visibility for classes (internal) which should work for your example.
You should to specify the visibility of classes explicitly. Have a look at: https://msdn.microsoft.com/en-us/library/ms173121.aspx