Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sandhillceltic · Nov 17, 2013 at 10:42 PM · javascriptvariablespublic variable

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....

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sandhillceltic · Nov 18, 2013 at 03:40 AM 0
Share

Thank you so much! Fixed the problem!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing variables from scripts issues 1 Answer

Accessing a js static var from a c# script 1 Answer

Inspector Assigned Variables Not In Both Scenes 1 Answer

Passing a variable to thread function in Javascript 1 Answer

move childA to position of childB 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges