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
1
Question by Chris 35 · Apr 13, 2011 at 09:23 AM · javascriptvariableclass

Access Variable in a class from another script

Hi

im using the platform controller that comes with unity and im trying to change the speed of the player within the game. the problem is that the speed var is located with in a class in the Player script and im having trouble accessing it from another script.

its located: Controller then in a class called ControllerMovement then speed.

how do i get to the speed var from another script

Code would be very helpful :) Thanks

EDIT: this is where it is, inside LanganController

#pragma strict

var canControl = true;

var spawnPoint : Transform;

class LangmanControllerMovement { // the class i cant access

var runSpeed = 7.0; // i want to change that Var from another script

and this is how i tried to change it from another script on another object :

// so you assign the player here in the inspector

var target :LangmanController;

function Update() {

 var temp = target.GetComponent("LangmanControllerMovment";

 temp.gravity = 100;

}

Thanks

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 HolBol · Jun 19, 2013 at 06:58 PM 0
Share

Cleaned up the code formatting for you.

4 Replies

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

Answer by AngryOldMan · Apr 13, 2011 at 10:45 AM

There are two ways around it. Depenging on how and why you want to increase the players speed. If you are using a "pickup" and want the character to speed up when he collects it then add a variable to the part of script that controls speed saying something like

if (HasPickup)
{
   Speed = Speed * 1.5;
}

or if you really need to access it from another script make it a public or static variable depending on what method you want to access it with.

Also get component works like this:-

// so you assign the player here in the inspector
var PlayerObject : GameObject;
//get the component from the player, in this case a script
TemporaryVariable = PlayerObject.GetComponent(PlayerScriptWithSpeedOn);
//do something to the speed variable on that script
TemporaryVariable.Speed = TemporaryVariable.Speed * 1.5
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 Chris 35 · Apr 13, 2011 at 11:32 AM 0
Share

i get this error "Object reference not set to an instance of an object"

avatar image AngryOldMan · Apr 13, 2011 at 12:10 PM 0
Share

thats because you just put the code in without modifying it to your needs or you havn't assigned the variable PlayerObject

avatar image Chris 35 · Apr 13, 2011 at 12:56 PM 0
Share

i changed it to my need but i cant assign PlayerObject any way because my Player isnt a prefab so i changed it to Transform ins$$anonymous$$d and i still get the same error // so you assign the player here in the inspector var target :LangmanController; function Update() { var temp = target.GetComponent("LangmanController$$anonymous$$ovment"); temp.gravity = 100;

} Ps its gravity i want to change not speed

avatar image AngryOldMan · Apr 13, 2011 at 02:04 PM 0
Share

update you post to include this relevant data its no good as a comment. then i'l provide some more help

avatar image Chris 35 · Apr 14, 2011 at 01:06 AM 0
Share

I updated my question and gave code so should be easier now i hope

avatar image
1

Answer by efge · Apr 13, 2011 at 09:25 AM

You could take a look at the reference for GetComponent.

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 Chris 35 · Apr 13, 2011 at 09:29 AM 0
Share

ive tried look at that but because pre new to unity i cant get it to work :( could you give mw the code i would need to access it ?

avatar image efge · Apr 13, 2011 at 11:02 AM 0
Share

Take a look at the link in my answer, there are code samples.

avatar image
1

Answer by Chris 35 · Apr 14, 2011 at 04:07 AM

ITS ALL GOOD EVERYONE I FIGURED IT OUT I WAS CALLING THE WRONG THING I WAS MEANT TO BE CALLING MOVEMENT.SPEED NOT THE MOVEMENT CLASS . SPEED

I PROBS DONT MAKE ANY SENCE BUT IT DONT MATTER COZ IT WORKS :)

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 Itinerant · Dec 18, 2012 at 06:10 PM 0
Share

Just want to leave a note on what Chris is talking about, because I was running into the same problem and beat my head over it for way longer than was necessary :P

So in these scripts, you define your class. This class may have a number of default values in it. You then define one or more variables of that class type. What he and I were doing was calling out to the class, and trying to change the variables there. What we should have been doing is finding that variable of that class type, and then changed that. Like this:

class Char$$anonymous$$otor$$anonymous$$ove{ var gravity : float = 10; }

var movement : Char$$anonymous$$otor$$anonymous$$ove = Char$$anonymous$$otor$$anonymous$$ove();

//In other script

getComponent(ScriptName).movement.gravity = whatever;

What we were doing was trying to go getComponent(ScriptName).Char$$anonymous$$otor$$anonymous$$ove.gravity = whatever, which doesn't work at all.

At least, that's what happened to me, and I'm pretty sure we did the same thing :)

avatar image
-1

Answer by robertmathew · Apr 13, 2011 at 10:53 AM

Let be your first script name called sample

with varibale

 var size : float = 10;

Let be your second script name called sample1

GetComponent(sample);

function OnGUI() {

sample.size = 20;

 }

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 AngryOldMan · Apr 13, 2011 at 12:15 PM 0
Share

No...just no. This has nothing to do with what Chris has asked about, he is accessing a variable on a script, I think I sort of get what you are trying to do here but the whole GUI function is wrong.

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

2 People are following this question.

avatar image avatar image

Related Questions

Accessing a variable inside a class inside other script... 2 Answers

Declaring & understanding Class in javascript 1 Answer

script needs to derive from monobehaviour 1 Answer

Enum style variable 2 Answers

How to Typecast JS Variables as C# Classes? 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