Home > Software engineering >  How do I add unity script settings?
How do I add unity script settings?

Time:04-24

how do I make a script, that when attached to an object, you can change the settings of it under the script, like a number value or string?

CodePudding user response:

You need to add the variables in public visibility.

Source: Variables and the Inspector

For instance:

using UnityEngine;
using System.Collections;

public class MainPlayer : MonoBehaviour 
{
    public string myName;
    
    // Use this for initialization
    void Start () 
    {
        Debug.Log("I am alive and my name is "   myName);
    }
}

You can now change "the settings" of the variable myName when attached to an object.

CodePudding user response:

There are basically two options, either making the variable public or using the [SerializeField] attribute. The best practice is to use [SerializeField] if you're not going to access the variable outside the class.

1: public string exposedString;

2: [SerializeField] private string serializedString;

  • Related