Add Component with Script Data

Hi, so I have a script like this,

 public void Player1()
    {
        var go = Instantiate(p1, new Vector2(-0.5f, -5), Quaternion.identity)as GameObject;
        go.AddComponent<PlayerMove>();
        go.AddComponent<PlayerAttack>();       
        DontDestroyOnLoad(p1);
        sm.NextCharacterSelect();
        
        }

And I am trying to add a script to an instantiated game object but whenever I add the script it doesn’t have any data that I already assigned in the inspector, for example changing the move speed to 10 and jump force to 5. So what I am asking is if there is any way I can add a script to an object but already have the stuff you need to assign in the inspector predetermined. I’m not sure if this makes sense but any help would be nice.

When a component is attached (from editor or by script), the assigned values are the ones you out when declaring the variables or the ones you assign in the Awake / Start method.

public int Lives = 5;
public float Score; // default value = 0
public string Name; // default value = null

private void Awake()
{
     Name = "unknown";
}

If you want to change the values of the variable when adding the component, either change them here, or assign them one by the one from the calling script or create a function to easily change them all.

public void Init( int lives, float score, string name )
{
    Lives = lives;
    Score = score;
    Name = name;
}

// In your other script

PlayerAttack attack = go.AddComponent<PlayerAttack>();
attack.Init( 10, 0, "John Doe");

Hello thanks for the response, I assigned the float and int values for my character movement in my script, and it works fine, however, I was wondering if there was a way to Automatically know my public Transform’s or Scripts that I am accessing from somewhere else. Sorry if you already mentioned the answer I’m kind of new to Unity and I don’t fully understand everything.

Hi I was kind of talking about this sort of stuff[137707-cafdspture.png|137707]

[137708-capturfde.png|137708]