- Home /
How to change variables on a new gameobject's script
So, I have a MapBuilder object that has a script which places random stars on the 2d plane. While generating each star, I want to give them some variables to make them unique, for example their color or size.
I made a script that gets added to the new star objects that stores those variables, but I need to change them from the MapBuilder script.
The stars start as a new empty GameObject. I then add the script via the AddComponent function. Here is where I encounter my problems: I try to change the variable of the scripts, but Unity returns an error: starColor is not a member of UnityEngine.Component.
I have seen other people sing this method for chnaging variables so i don't know why it is not working.
This is the script in the MapBuilder object:
#pragma strict
var amtStars = 1;
function Start () {
for (var i = 0; i < amtStars; i++){
var star = new GameObject("Star" + (i + 1));
var inst = GameObject.Find("Star" + (i + 1));
inst.AddComponent("starControler");
var script = inst.GetComponent("starControler");
script.starColor = "red";
//star.transform.position = transform.position;
//star.AddComponent("SpriteRenderer");
}
}
This is the script in the new star, it is just there to store the variable:
#pragma strict
public var starColor : String;
function Start(){
Debug.Log(starColor);
}
Answer by Baste · Sep 26, 2014 at 08:20 PM
You need to tell your script that the component you get is a starControler:
inst.AddComponent("starControler");
var script = inst.GetComponent("starControler") as starControler;
script.starColor = "red";
Since AddComponent actually returns the added component, you can cut it down and make it more readable:
var script = inst.AddComponent("starControler") as starControler;
script.starColor = "red";
That should work, though my UnityScript isn't the sharpest. As an additional pet peeve - controller is written with two l's, and classes should be capitalized, so it should be StarController, not starControler.
By the way, I'm really not very good at UnityScript. If that doesn't work, try using the GetComponent method like they do in the docs, here.
Thanks for your input, but the method you proposed does not work for me, i now get the error: The name 'starControler' does not denote a valid type ('not found').
Also the GetComponent method gave me the same error as before
The "not a valid type" error is because the compiler can't find a type named "starControler". Replace it with the actual name of the script you're adding.
Ah thanks, i made a typo. Stupid me! Anyway it works, many thanks!
That's why it's better to use generics ins$$anonymous$$d of the string form in just about all cases.
Your answer
Follow this Question
Related Questions
how can I display a variable as a GUIText 5 Answers
Scripting error! 1 Answer
Using variable from one script attached to one object in another script attached to another object 0 Answers
How can I access a variable from another script in a separate object? 1 Answer
How to access gameObject variable script 2 Answers