- Home /
Editing external vars from another script problem?
So I have a game where planes fly from one town to another, and they can carry disease with them. The disease is randomly given to a town at the start of the game, allowing it to spread to the rest of the towns. Here are the scripts I have right now.
City script (City creation.js)
private var minimumpeople = 1000; //technical vars, determining the max and min # people in the city
private var maximumpeople = 10000;
var people = 0; //the number of people in the town (randomly generated in function Start ())
public var percentinfected = 0.0; //the percent of people in the town that are infected
var airplaneprefab : GameObject; //the airplane prefab
var dmanager : GameObject;
var maincam : GameObject;
var objectpos : Vector3;
var cursorpos : Vector3;
function Start () { //things that happen only once, in the first frame
people = Random.Range(minimumpeople, maximumpeople); //the random generation of the number of people in the city
dmanager = GameObject.FindGameObjectWithTag("Dmanager"); //seting the disease manager var
maincam = GameObject.FindGameObjectWithTag("MainCamera"); //setting the main camera var
}
function Update () {
if(Random.Range(0, 1000) < 1) { //the controller on how often a plane takes off
var jsplane = Instantiate(airplaneprefab, transform.position, transform.rotation); //creating the airplane
jsplane.GetComponent("Airplane").takeoff = gameObject.name; //setting that the town that the plane is coming from
if(percentinfected > 1) {
jsplane.GetComponent("Airplane").infected = true;
}
}
percentinfected = percentinfected * dmanager.GetComponent("DMscript").infectivity; //retreiving the infectivity rate from the disease manager
objectpos = maincam.camera.WorldToScreenPoint(transform.position); //calculating the locations of the mouse, and the positions of the cities on the screen
cursorpos = Input.mousePosition;
if(percentinfected > 1) { //if the town is infected, then it will give off particals (this is for beta testing)
particleSystem.emissionRate = 3;
}
}
Airplane script (Airplane.js)
#pragma strict
private var allcities : Array; //an array of all the cities on the map
var takeoff : String; //where the plane has taken off from
var landing : String; //where the plane will land
var landingcity : GameObject;
public var infected : boolean = false;
function Start () {
allcities = GameObject.FindGameObjectsWithTag("City"); //putting all the cities on the map into an array
landingcity = allcities[Random.Range(0, allcities.length)];
landing = landingcity.gameObject.name;
}
function Update () { //part of the script that makes the plane actualy fly to the destination
transform.LookAt(landingcity.transform); //turn to face the landing city
transform.Translate(0, 0, 0.025); //proceed foreward
if(Vector3.Distance(transform.position, landingcity.transform.position) < 0.3) { //if the airplane is inside the city, it gets destroyed
Destroy(gameObject);
}
if(Vector3.Distance(transform.position, landingcity.transform.position) < 0.4) { //if the airplane is inside the city, it gets destroyed
landingcity.GetComponent("City creation").percentinfected += 1;
}
}
And, finally, Disease manager (DMscript.js) (this one isn't quite as important, but I decided to include it anyway)
var infectivity = 0.0; //how easiliy the disease spreads
var visibility = 0; //how noticable the disease is
var accessibility = 0; //how easy the disease is to research
var allcities : Array; //an array of all the cities on the map
private var firstinfected : GameObject;
function Start () {
infectivity = Random.Range(1.0, 1.001); //randomizing the disease vars
visibility = Random.Range(0, 100);
accessibility = Random.Range(0, 100);
allcities = GameObject.FindGameObjectsWithTag("City");
firstinfected = allcities[Random.Range(0, allcities.length)];
firstinfected.GetComponent("City creation").percentinfected = 1;
}
I keep getting the error
Assets/Standard Assets/Scripts/Airplane.js(26,59): BCE0019: 'percentinfected' is not a member of 'UnityEngine.Component'.
which doesn't make any sense....
Answer by ArkaneX · Nov 18, 2013 at 12:23 AM
On the contrary - this makes perfect sense. You're calling non generic GetComponent
overload which returns Component
class, instead of instance of your specific class. And Component
class does not have percentinfected
property. You have to call different overload:
GetComponent(Citycreation) // no quotes
If you create script name with space inside, then resulting class name will have this space removed, thus actual class name will be different. I advice to change your script name to CityCreation.js, and to avoid spaces in the names of your future scripts.