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 Pflobus · Jan 14, 2015 at 04:23 AM · movementweapon

Calling Other Variables from Other Scripts.

So, as the title says, I have a slight dilemma regarding calling variables from other scripts. So, I have one script, where you can switch objects, and when one object is selected, then, it should call on another script, a move script, to change the speed. So, here are my scripts:

var Obj : Transform;

function Start () { // Select the first weapon SelectWeapon(0); }

function Update () { // Did the user press fire? if (Input.GetButton ("Fire2")){ print("YO!"); }

 if (Input.GetKeyDown("1")) {
     SelectWeapon(0);
 }    
 else if (Input.GetKeyDown("2")) {
 SelectWeapon(1);
 //this is where I would call the var to change the runspeed
 
 }
 else if (Input.GetKeyDown("3")) {
 SelectWeapon(2);
 }
 }
 
 
 

function SelectWeapon (index : int) { for (var i=0;i


pragma strict

var walkSpeed: float = 7; // regular speed var crchSpeed: float = 3; // crouching speed var runSpeed: float = 20; // run speed

 private var chMotor: CharacterMotor;
 private var tr: Transform;
 private var dist: float; // distance to ground
  
 function Start(){
 chMotor = GetComponent(CharacterMotor);
 tr = transform;
 var ch:CharacterController = GetComponent(CharacterController);
 dist = ch.height/2; // calculate distance to ground
 }
  
 function Update(){
  
 var vScale = 1.0;
 var speed = walkSpeed;
  
 if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
 speed = runSpeed;
 }
 if (Input.GetKey("c")){ // press C to crouch
 vScale = 0.5;
 speed = crchSpeed; // slow down when crouching
 }
 chMotor.movement.maxForwardSpeed = speed; // set max speed
 var ultScale = tr.localScale.y; // crouch/stand up smoothly
 tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
 tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
 }




Any help would be greatly appreciated. Thanks in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ransomink · Jan 15, 2015 at 01:54 AM

First off, no one can help if you can't properly tell us the names of your scripts. Adequate info is necessary. Secondly, @ThomasMarsh gave you the correct answer for accessing a variable from another script.

Inside of the script called "CharWeapon" (the actual script you are calling from)

"The other script which I call from is called something like charweapon or something like that."

create a private variable with the same name as the script you want to access, but in lowercase.

 private var move : Move; // Reference to the Move script

Then setup the instance to the script in the Awake () function.

 function Awake ()
 {
    move = GetComponent ( Move ); // Set a reference to the Move script
 }

In your code you have...

 move = GetComponent ( Player );

... which is incorrect because "Player" is the gameobject, not the script you're trying to access. There are also other factors at play: the "CharWeapon" and "Move" script must be attached to the "Player" gameobject, and "runSpeed" has to be a public variable in order to access it.

You do not have to cast a type to "runSpeed", you already - should have anyway - done so inside the "Move" script. Also, using 50.0f is C# syntax; with UnityScript it is not needed. The code should be as follows...

 move.runSpeed = 50.0;

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 Pflobus · Jan 15, 2015 at 03:13 AM 0
Share

After reading the rest of my comments, you should see that I updated. But it still doesn't work.

avatar image ransomink · Jan 16, 2015 at 02:51 AM 0
Share

Post both of your scripts, so we can see all lines. I tested this and it works 100%. Hopefully we'll see where the issue lies...

avatar image
0

Answer by ThomasMarsh · Jan 14, 2015 at 04:39 AM

The syntax I personally use when accessing other scripts is this:

 private var enemySight: EnemySight;// EnemySight is name of the script
 
 function Awake(){
     enemySight = GetComponent(EnemySight);// A reference to get script from object it is in
 }

And then to access a public variable from that script, you would use enemySight.playerInSight where playerInSight is a variable in the EnemySight script. It must be public to access it. I hope this is of use to you.

Comment
Add comment · Show 11 · 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 Pflobus · Jan 14, 2015 at 04:54 AM 0
Share

So, here's what I changed it to (it doesn't work-no errors, but nothing really changes...note that I changed the float from 20 to 50...):

var obj : Transform;

private var move : $$anonymous$$ove;

function Start () { // Select the first weapon SelectWeapon(0); }

function Awake() {

move = GetComponent(Player); }

function Update () { // Did the user press fire? if (Input.GetButton ("Fire2")){ print("YO!"); }

 if (Input.Get$$anonymous$$eyDown("1")) {
     SelectWeapon(0);
 }    
 else if (Input.Get$$anonymous$$eyDown("2")) {
 SelectWeapon(1);
 
 move.runSpeed : float = 50.0f;
 
 }
 else if (Input.Get$$anonymous$$eyDown("3")) {
 SelectWeapon(2);
 }
 }
 
 
 

function SelectWeapon (index : int) { for (var i=0;i

avatar image ThomasMarsh · Jan 14, 2015 at 07:02 AM 0
Share

In the player script, try changing the run speed variable to public.

avatar image ThomasMarsh · Jan 14, 2015 at 07:03 AM 0
Share

Also, is player the name of the script? Whart are the names of the two different scripts?

avatar image Pflobus · Jan 14, 2015 at 02:01 PM 0
Share

No, player is the name of the gameobject that the script is in. The actual script is called "$$anonymous$$ove". That's the one I want to call. The other script which I call from is called something like charweapon or something like that. Do you know if you could apply the answer as an example in my script?

avatar image Pflobus · Jan 14, 2015 at 02:02 PM 0
Share

And the runSpeed is public.

Show more comments
avatar image
0

Answer by ramp · Jan 14, 2015 at 05:01 AM

Hello,

Accessing the variable from one to another script,then first you find the name of GameObject in Hierarchy and then after that script name (i.e GetComponent),just like this.

chMotor = GameObject.Find("").GetComponent("CharacterMotor");

may be help.

Thanks

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 Pflobus · Jan 14, 2015 at 01:58 PM 0
Share

No errors, but nothing different happened.

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Sprinting Audio Problem 1 Answer

Incremental Move and stop 1 Answer

can someone please take a look at this movement script? 3 Answers

Add a slight rotation when moving horizontal 0 Answers


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