0

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?

1
  • 1
    just add a public variable in the script, Unity will do the rest. Technically, Unity will only show serializable types, but the basic types are all serializable. Commented Apr 23, 2022 at 14:17

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.