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.
Answer by Hellium · May 06, 2019 at 05:08 AM
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.
I don't really understand your question:
I was wondering if there was a way to Automatically know my public Transform's or Scripts that I am accessing from somewhere else
Could you provide an example and/or drawing?
I am sorry, I still don't understand everything.
$$anonymous$$aybe the several "solutions" betlow is what you want
Supposing you have a Spawner
script responsible for instantiating your players. The Player
can have a reference to the spawner like this:
var go = Instantiate(p1, new Vector2(-0.5f, -5), Quaternion.identity)as GameObject;
PlayerScript player = go.AddComponent<PlayerScript>();
player.Spawner = this; // this is a keyword meaning the current instance running the code
If you want your player to be aware of the Scene$$anonymous$$anager
, referenced by the spawner, the logic is the same
public Scene$$anonymous$$anager sm;
// ...
var go = Instantiate(p1, new Vector2(-0.5f, -5), Quaternion.identity)as GameObject;
PlayerScript player = go.AddComponent<PlayerScript>();
player.Scene$$anonymous$$anager = sm;
If you want your player to get a reference to another object, not referenced by its spawner, you can use the FindXXX
function according to your needs
// In the player script
public void Start()
{
SomeGameObject = FindObjectOfType<ScriptName>();
}