Cant display array items in the inspector. Unity - c#

I want to make a class array and see all these classes items in the inspector.
I have 2 scripts. 1 -set to prefab and requires MonoBehaviour to be included.
2 - script where I create the array but in the inspector, I see only Element0, Element1...
When I remove MonoBehaviour from the 1st script I'm able to see all the items in the inspector, but that way it doesn't work with the prefab...
1-
[System.Serializable]
public class LevelSetup : MonoBehaviour
{
public TextMeshProUGUI levelName;
public Image levelImage;
public bool locked;
public GameObject Description;
public string Text;
}
2-
public class LevelSpawn : MonoBehaviour
{
public LevelSetup[] levels;
Want to be displayed array with all the "LevelSetup" fields (that are public), but if I leave the MonoBehaviour and it works fine with prefab it displays array with the only Element0, Element1, etc.
Thank You!

When you put a field that inherits from MonoBehaviour the Unity's inspector expects an object that has "Behaviour", so you cannot assign the fields in the inspector because is not interpreted as a regular field.
For example, if you set a Material as a field, and you want to change the color property from the inspector.
public Material mat; //This is actually a class
In the inspector you will only be able to assign a material but you won't be able to access inside the class and change the color.

Related

Unity fill array Gameobject from another script

I have been follow video from youtube for creating custom tabs. But in condition Iam using dynamic panel, and then in script has to fill panel to array Gameobject objectsToSwap. My Question how I fill that array from another script ?
enter image description here
Let's:
-call the script with the Array = A
-call the script with the GameObjects = B
You need to get a reference to the script A on the script B.
Then you could do something like this:
public class A : MonoBehaviour
{
private GameObject[] _array;
public void AssignObjects(GameObject[] objects)
{
_array = objects;
// if you're doing this on the editor then you also want to set A as dirty to make sure the changes are saved
// also need to include #if UNITY_EDITOR so this doesn't get compiled on builds
// this can't be compiled because the UnityEditor namespace does not exist on builds
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
}
public class B : MonoBehaviour
{
[SerializeField] private GameObject[] objects;
[SerializeField] private A otherScript;
[ContextMenu("Assign Objects")] // in case you want to do this using the context menu (click the 3 dots on the inspector of this script)
private void AssignObjects()
{
AssignObjects(otherScript);
}
public void AssignObjects(A script)
{
script.AssignObjects(objects);
}
}
Another option if you have access to all those fields in the editor, just right click on the origin array => Copy then right click on the desired array => Paste

Accessing and Assigning TMP_Text on Start

I am currently using the Inspector to assign each text element but I would like to assign them through code instead in my start method.
How do I find the component by name because I have more than one.
I need each component to match up with the variables I have created.
public class Timer : MonoBehaviour
{
public TMP_Text p1Name;
public TMP_Text p2Name;
public TMP_Text p1Score;
public TMP_Text p2Score;
private void Start()
{
}```
If you want to grab them by name use the line
GameObject.Find("NameHere")
Then grab the component you need
TMP_Text yourText = null;
private void Start()
{
yourText = GameObject.Find("nameHere").GetComponent<TMP_Text>();
}
If all of your text objects are childed to the same parent, you can also iterate over the transforms of the parent instead of the larger Find and use Transform.Find. Is there a reason you don't want to assign them in the inspector though? Assigning a reference instead of finding them in Start is faster.

how to configurate nested object in Unity's inspector

I tried to create nested object in C# script in Unity and change the values in it.
Class of nested object
using UnityEngine;
public class Replica : ScriptableObject
{
public string Text;
public int Speed = 1;
public int PersonaID = 0;
}
Class with nested object
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/DialogContainer", order = 1)]
public class DialogContainer : ScriptableObject {
public Replica[] Replicas; // Here is nested object
public Sprite[] Avatars;
}
And when I created the ScriptableObject I saw that:
Photo of ScriptableObject interface
Here I can only put an instance of the class there, but I cannot configure it.
But i want to change values right in inspector without creating and inserting object of class "Replica" like in InputManager where I can create one more obj in axis, open it and change values in inspector like that.
Photo of interface I want to see
If u want configurate your class in inspector, that class must be not derived from MonoBehaviour or ScriptableObject and be with tag [System.Serializable] than u can serialize it in nested class without creating instance.
Replica:
[System.Serializable]
public class Replica
{
public string Text;
public int Speed = 1;
public int PersonaID = 0;
}
Dialog Container:
using System.Collections.Generic;
using UnityEngine;
public class DialogContainer : MonoBehaviour{
public List<Replica> Replicas;
public Sprite[] Avatars;
}
Than I saw that:Inspector screenshot
Unless you make a custom editor for DialogContainer, you can't do what you want to do. If you have a parent ScriptableObject that contains a child list of ScriptableObjects, you can't edit each child's fields when you have the parent selected in your Project View.
You will have to edit each child Replica by selecting the Replica instance in the Project View. Then the Inspector will let you edit that Replica.

Multiple characters in videogame using Unity

I am developing a videogame using Unity and I want the user to be able to choose from a group of characters who have different attributes and weapons.
Do I have to create a scene for every character for each level or there is a dynamic way of doing it?
Your scripts :
GameManager :
public class GameManager : MonoBehavior
{
public static GameObject character; // this field is to remember the selection. Since it is 'static', it will keep value in between scenes.
}
LevelManager :
public class LevelManager : MonoBehavior
{
public void Start()
{
// read info stored in GameManager
GameManager.character.transform.position = /* your start position */
}
}
In scene 1 (selection):
store charcter selected in GameManager.character
In scene 2 (level):
An empty object with LevelManager script.
Note: Since we use static, I'm actually unsure that you even need the GameManager script to be attached to an empty object, for both scenes.
In the selection scne, just make sure to assign the proper thing to character (instance of Zelda or Megaman character prefab)

Unity3D/C# - add a variable to gameobject

How can I add a variable to a GameObject?
GameObjects have a series of variables (name, transform, ...) that can be accessed and modified from any script. How could I add a variable such as for example "color" or "type" that could be accessed or modified from other scripts.
Thanks.
If GameObject wasn't a sealed class then inheritance could have been the solution to your problem. In this ideal situation you could try the following.
public class ExtraGameObject : GameObject
{
// Logically you could make use of an enum for the
// color but I picked the string to write this example faster.
public string Color { get; set; }
}
A solution to your problem could be to declare your class like below:
public class ExtraGameObject
{
public string Color { get; set; }
public GameObject GameObject { get; set; }
}
I am aware of this that this is not exactly that you want, but that you want I don't think that it can be done due to the sealed nature of GameObject.
As every object in c# GameObject class inheritance Object class. If you right click on GameObject and select go to definition you will see that below.
In order to reach one object's variables you need a reference(not considering static variables). So if you want to change variables of one GameObject from other you need a ref.
On Unity inspector you can assign values to public variables of a script. Below you can see Obj end of the arrow. Now assign cube(is a GameObject ) to Obj.
After drag and drop
Now you can reach every subComponent of cube object. In red rectangle I reached it's Transform component and changed it's position.
I hope I understood your question correct.GL.
Christos answer is correct. You cannot add a variable to the GameObject datatype because it was built into the language as a base. To add variables to premade data type, extend off of them. For more information on inheritance see
https://msdn.microsoft.com/library/ms173149(v=vs.100).aspx
You can add a script to the prefab from which an object is created using the Instantiate method. In this script, you declare public variable.
Then you can change and get the value of this variable using the GetComponent method.
The code in script file added to the prefab (prefab name is thatPrefab, script file name is IdDeclarer) as a component:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IdDeclarer : MonoBehaviour
{
public int id;
}
Declaring an object's characteristics (ID in example):
GameObject newObject = Instantiate(thatPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
newObject.GetComponent<IdDeclarer>().id = 99;
Debug.Log(newObject.GetComponent<IdDeclarer>().id);
Output: 99
I hope I helped you

Categories