• 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 MikeErty · Jul 18, 2013 at 03:43 PM · accessing from any script

Accessing var in another script not working!

I finished a full game a few weeks ago and I used this function a lot, accessing and changing vars from lots of different scripts but for some reason I just cannot seem to do it again on this project.

 var player : GameObject;
 
 function Start()
 {
      player = GameObject.Find("TEST_Player1");
 
 }
 
 function stealthMod()
 {
     
     if (player.GetComponent(Footsteps).stealthy == true)
     {
         Debug.Log("slowing down");
     }
 }

I've tried so many variations of the player.GetComponent line. As it is, this code brings up this error: Unknown identifier: 'Footsteps'. The Footsteps script is attached to TEST_Player1. I'm sure it'll be something simple, maybe I just need fresh eyes on it...

Oh and i'm sure it's obvious what i'm trying to do but just in case - I want to check if the var 'stealthy' is true in 'Footsteps' so I can then modify the speed values which are in the current script. (I didn't include all the character motor script for simplicity.) Also I'm aware that just having this in its own function won't make it activate, i'll have to put it in update or something later but for now I just want the code to be error-free.

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
0
Best Answer

Answer by MikeErty · Jul 18, 2013 at 03:59 PM

Stupid stupid stupid, so simple. When I brought in the FPS controller from standard assets it created a new 'scripts' folder in which is kept the script I'm using here. I had already made another folder in my project root for my scripts and was putting all of the scripts I made in there. When it came to communicating from a script in one folder to a script in another folder it wasn't happy. Moral of the story - keep all your scripts in one place!

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
1

Answer by amphoterik · Jul 18, 2013 at 03:45 PM

You need to capitalize the G:

 GameObject.Find("TEST_Player1");
Comment
Add comment · Show 3 · 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 MikeErty · Jul 18, 2013 at 03:51 PM 0
Share

Thanks for looking and hello again! That was one of the variations I tried and it still gives me the same error. I edited it in the OP to match my script (its now a capital G) so we're on the same page. Anything else it could be?

avatar image amphoterik · Jul 18, 2013 at 03:52 PM 1
Share

What is the name of the Footsteps script exactly? Check spelling and capitalization. Also, make sure your player's name is TEST_Player1 in the Unity hierarchy view.

avatar image MikeErty · Jul 18, 2013 at 04:00 PM 0
Share

Again thanks for taking the time to help me. I knew it was a stupid error on my part and I couldn't see what was right in front of me.

avatar image
0

Answer by Krish-Vikram · Sep 19, 2013 at 06:02 PM

if its a static variable, you could directly access. scriptname.variablename;

Initial_force.js:

static var score : int=0;

score_display.js:

function start() { guiText.text= "Score: " + Initial_Force.score; }

this worked for me ...

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Create Array Accessible By Any Script 2 Answers

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

how do i change a bool from another script 1 Answer

Access UI in scene from an instance prefab 1 Answer

How do I use Raycast and GetComponent to run a function in a script attached to the hit object (in C#)? 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