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 cipherr · Jul 12, 2012 at 01:14 AM · javascriptvariablesclassesothercalling

Calling variables in classes from other scripts

Hello. I'm a new unity user, and am just messing around with some basic scripting concepts before I get started on any real projects

Anyways, what I want to do is make a simple powerup that when touched, makes you run faster. I'm using the built in first person camera, and created a separate script for the powerup itself. I'd like to call the 'maxForwardSpeed' variable from the 'CharacterMotor' js file. Note that is part of the CharacterMotorMovement class in said file. This is what I have so far (I know what I have is wrong)

 function OnTriggerEnter(other : Collider) {
 if (other.tag == "Player") {
  other.GetComponent(CharacterMotor).maxForwardSpeed = 1;
     Destroy(gameObject);

 }

}

As a disclaimer, I have checked every question here related to this topic and have read the piece on this in the reference manual (link below) multiple times

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stingman · Jul 12, 2012 at 01:42 AM

I assume this script will be attached to the powerup. If that's the case this looks fine as is. This is what your code will do:

when a game object tagged as "Player" enters the trigger of the powerup it will look for a component on "Player" called CharacterMotor. It will then change the value of the variable of maxForwardSpeed to equal 1. It will also destroy the powerup.

Is this what you want? If 1 is the default value of the variable then just increase the value until you reach the desired speed. You've got it all set up correct though.

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 cipherr · Jul 12, 2012 at 03:25 AM 0
Share

Yeah it's attached to the powerUp. I'll keep messing around with it some more, because its giving me a "$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'Character$$anonymous$$otor.maxForwardSpeed" error. I should note that this only comes up when you touch the powerup item, no errors before that. Thanks though, at least I know I'm close hah

avatar image
0

Answer by fifty6 · Jul 12, 2012 at 04:49 AM

var foodDec: int = 0;

function Update() {

var motorScript = transform.GetComponent(CharacterMotor);

if(Input.GetKey("left shift")) { //Debug.Log("player is running"); motorScript.movement.maxForwardSpeed = 9; foodDec = 5;

} else { motorScript = transform.GetComponent(CharacterMotor); motorScript.movement.maxForwardSpeed = 6; foodDec = 1; }

}

This is how I created a sprint function. Try calling it by variable.movement.maxForwardSpeed = .

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 cipherr · Jul 15, 2012 at 12:17 AM 0
Share

Thanks for the tip. While I still haven't gotten it quiet yet, I know I'm close. I'll keep trying things

function OnTriggerEnter(powerUp : Collider) {

var moveScript = transform.GetComponent(Character$$anonymous$$otor);

if (powerUp.tag == "Player") { Debug.Log("get"); moveScript.movement.maxForwardSpeed = 50; Destroy(gameObject); } }

is what I'm now using, but I keep getting a null reference exception error

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

6 People are following this question.

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

Related Questions

Calling variables from other scripts 2 Answers

Organizing variables in the inspector 5 Answers

How do you return variables from other scripts? 2 Answers

Linking prefabs to classes that are not attatched to gameobjects 1 Answer

if statement inside variables of a class 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