Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Airmand · Jul 09, 2011 at 04:04 PM · variablecomponentaccesscallbce0020

Accessing a variable of a script from another scene.

Ok so what i have is 2 seperate scenes. The actual "game scene" where you kill things and pickup points. I have it so that once you have picked up all the points in that scene you will go to the Shop Menu through Application.LoadLevel. In the shop menu right now there is 1 button which i am trying to make it so that the variable of the "shoot" script in the "game scene" that determines how far you shoot will be increased if you have enough points. I also want that increase to stay on future levels like an upgrade. The errors I am getting with the ShopMenu script is

Assets/Scripts/ShopMenu.js(15,34): BCE0020: An instance of type 'Points' is required to access non static member 'score'.

Assets/Scripts/ShopMenu.js(17,35): BCE0020: An instance of type 'shoot' is required to access non static member 'gunRange'.

My Shop Menu script is

function OnGUI () { GUI.Box (Rect (10,10,250,90), "Shop Menu");

 if (GUI.Button (Rect (20,40,200,20), "Longer Range:   15 Points  ")) {
 
     Debug.Log ("Bought Longer Range");
 
     BuyRange();
 }

}

function BuyRange() {

 var points = gameObject.Find("Player").GetComponent(Points);
 
 var pointsscore = Points.score;
 
 var shootrange = gameObject.Find("TankTurret").GetComponent(shoot);
 
 var shootingrange = shoot.gunRange;
 
     if (pointsscore >= 15) {
 
     shootrange.gunRange = 50;
 }

}

The shoot script is var gunRange = 40; var bullet : Rigidbody; function Update () { if (Input.GetKeyDown ("space")) { var clone : Rigidbody; clone = Instantiate(bullet, transform.position, transform.rotation); //Give the cloned bullet velocity along z axis clone.velocity = transform.TransformDirection (Vector3.up * gunRange);

 }

}

The Points script var score = 0;

function OnTriggerEnter ( other : Collider ) { if (other.tag == "Points") { score += 5; Destroy (other.gameObject); } }

function OnGUI (){ GUI.color = Color.red; GUI.Box (Rect(25,25,100,30), "Points: "+score); }

function Update () {

 if (score==15) {
     loadShopMenu();
 }

}

function loadShopMenu () {

 yield WaitForSeconds(3);
     Application.LoadLevel(2);

}

Comment
Add comment · Show 1
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 Airmand · Jul 09, 2011 at 04:27 PM 0
Share

sorry i dont know how to put code in properly i tried the greater pre lesser at the beginning of the scripts and the greater code lesser at the end of the scripts but that didnt work

7 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by aldonaletto · Jul 09, 2011 at 04:23 PM

I'm using static vars to hold things like points, lives, health etc. These variables are defined in a script called Control.js, and they may be accessed as Control.points, Control.health etc. This script is attached to my player (a FPS controller), and their static variables survive during the whole game:

 static var gunRange: float = 100;

Another alternative is to keep these variables in a script assigned to an empty object, and include the DontDestroyOnLoad instruction in its Awake function:

 var gunRange: float = 100;
 
 function Awake () {
     DontDestroyOnLoad (transform.gameObject);
 }

EDITED: If you want to use the static variable alternative, your scripts should look like below:

The ShopMenu.js script:

 function OnGUI () { 
     
     GUI.Box (Rect (10,10,250,90), "Shop Menu");
     if (GUI.Button (Rect (20,40,200,20), "Longer Range:   15 Points  ")) {
         Debug.Log ("Bought Longer Range");
         BuyRange();
     }
 }
 
 function BuyRange() {
 
     if (Points.score>=15){ // if the player has enough points...
         shoot.gunRange = 50;  // buy the new range...
         Points.score -= 15;  // and subtract the points spent
     }
 }

The shoot.js script:

 var bullet : Rigidbody;
 static var gunRange = 40;  // this variable may be accessed anywhere as shoot.gunRange
 
 function Update () {
 
     if (Input.GetKeyDown ("space")) {
         var clone : Rigidbody;
         clone = Instantiate(bullet, transform.position, transform.rotation);
         //Give the cloned bullet velocity along z axis
         //(is it correct? the bullet will go the Up direction of the turret!)
         clone.velocity = transform.TransformDirection (Vector3.up * gunRange);
     }
 }

The Points.js script:

     static var score = 0;  // this variable may be accessed as Points.score anywhere
     
     function OnTriggerEnter ( other : Collider ) { 
     
         if (other.tag == "Points") { 
             score += 5; 
             Destroy (other.gameObject); 
         } 
     }
     
     function OnGUI (){ 
     
         GUI.color = Color.red; GUI.Box (Rect(25,25,100,30), "Points: "+score); 
     }
     
     function Update () {
     
         if (score==15) {
             loadShopMenu();
         }
     }
 
     function loadShopMenu () {
 
         yield WaitForSeconds(3);
         Application.LoadLevel(2);
     }

Comment
Add comment · Show 5 · 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 Airmand · Jul 09, 2011 at 04:31 PM 0
Share

oh ok i didnt quite know what static variable did. I changed the gunRange and points variables to static variables and fixed some of the code that was wrong in Shop $$anonymous$$enu but now i get NullReferenceException Shop$$anonymous$$enu.BuyRange () (at Assets/Scripts/Shop$$anonymous$$enu.js:14) Shop$$anonymous$$enu.OnGUI () (at Assets/Scripts/Shop$$anonymous$$enu.js:9)

avatar image aldonaletto · Jul 09, 2011 at 05:54 PM 0
Share

A variable declared outside any function with var or public var is a public variable. It has script scope, what means that it's known by everybody in that script, but is a complete strange for other scripts. Static variables, on the other hand, have program scope, which means they are known by every script in the game. They also live during the whole game, even when a new level is loaded.
But there is another very important difference: a static variable is unique, while a public variable exists in each instance of the script where it's defined. If you have a variable defined as var ammo in a weapon script, for instance, and you have two of these weapons in scene, each one will have its own instance of ammo. If you decrement ammo in one weapon, the ammo instance of the other weapon will not be altered. They are like the Tornado Twins: if you kill one of them, the other will continue creating tutorials!
If that ammo variable were declared as static var ammo, on the other hand, both weapons would share the same ammo: if you decremented it in one weapon, the other would watch its ammo getting down too.
Well, that's the difference between static and public variables. I'm checking your code and will be back soon!

avatar image aldonaletto · Jul 09, 2011 at 06:25 PM 0
Share

Edited the answer to show how to use the static variables in your scripts. Remember that static variables are unique. If you have more than one TankTurret object, all of them will share the same gunRange, since it's a static variable now.

avatar image Airmand · Jul 09, 2011 at 07:57 PM 0
Share

im sorry i didnt even see the comments you were leaving. I believe the scripts you edited of $$anonymous$$e work now. I just have to create a level 3 and see if the gunRange increased after it was bought

avatar image Airmand · Jul 09, 2011 at 07:59 PM 0
Share

and thank you for the explanation. it is much clearer now

avatar image
0

Answer by Babilinski · Jul 09, 2011 at 04:44 PM

if(points = 100) { uprange up = (Range).GetComponent("Range"); eh.uprange(+10); }

and in your ange class you should have

uprange + range = currange

Comment
Add comment · 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
0

Answer by Airmand · Jul 09, 2011 at 05:07 PM

sorry im very new to coding and unity. What would my range class be. and im not sure how to read psuedocode. :P

Comment
Add comment · Show 5 · 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 Babilinski · Jul 09, 2011 at 05:20 PM 0
Share

if(points = 100) // your if block............................................................................................................ uprange up = (Range).GetComponent("Range")// replace "Range" with the code name that has the range in it........................................................................................................................................................ eh.uprange(+10); // this adjusts the value of uprange..........................................................................

in the script where you have the range int. you should have uprange + range = cur.range;

avatar image Airmand · Jul 09, 2011 at 05:32 PM 0
Share

i dont understand what uprange is

avatar image Babilinski · Jul 09, 2011 at 05:37 PM 0
Share

its a variable that i made up . exp.

your range is 5 then you go to the shop and you update your range buy 3 so

3 + 5 = currange A$$anonymous$$A curent range

avatar image Airmand · Jul 09, 2011 at 05:47 PM 0
Share

im sorry i just really dont know how to read your pseudocode. What is cur.range supposed to be?

avatar image Babilinski · Jul 09, 2011 at 05:53 PM 0
Share

okay currange = a value that will change as the character gets more range. uprange = a value that will be the amount of range added to your currange after you have enough points range = a value that is the starting value of range when you start the game;

avatar image
0

Answer by Airmand · Jul 09, 2011 at 06:06 PM

ok so i need to change the code in shop menu from if (pointsscore >= 15) { to if (pointsscore >=15) { uprange up = (Range).GetComponent("Range").uprange(+10); ? i really am not sure how to code the rest of what you said to do...

Comment
Add comment · Show 2 · 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 Babilinski · Jul 09, 2011 at 06:22 PM 0
Share

do you have skype? ill show you

avatar image Airmand · Jul 09, 2011 at 06:34 PM 0
Share

i can download it but i dont know how long it will take. Im currently in afgahnistan so my internet is a bit slower.

avatar image
0

Answer by Babilinski · Jul 09, 2011 at 06:36 PM

ahh well are you using java or C#?

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 Airmand · Jul 09, 2011 at 06:37 PM 1
Share

javascript

  • 1
  • 2
  • ›

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Storing transform information in a variable? 1 Answer

Call a method whenever the assigned variable gets altered 2 Answers

Collision object and access to a script variable? 1 Answer

How to access a variable in a script on the same object. 1 Answer

C# access another C # without using "GetComponent"? 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