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 RafaelCN · Oct 28, 2013 at 05:01 AM · javascriptvariableclassclassesaccessing from any script

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

Hello everybody, i've seen a lot of questions related to that question. I'll ask about access a variable inside a class from other script from other gameObject, such access a script from a enemy for a player, but how I do that? I've tried:

 var motor: Character;
 var playerDistance : Vector3;
 
 function Start () {
     motor = GetComponent(Character);
     
     playerDistance = motor.playerPosition - transform.position;
     
 }

As all of you can see, my playerPosition is a variable inside a class inside my script of the player. I cannot access it, this error appears:

 NullReferenceException: Object reference not set to an instance of an object

So I tried this one:

 var motor: Character;
 var playerDistance : Vector3;
     
 function Start () {
    motor = gameObject.GetComponent(Character);
         
    playerDistance = motor.playerPosition - transform.position;
         
 }

I see in the inspector the motor variable, I've tried to put my player inside it, but when I execute the game, the variable clean itself and throw the error.

alt text

I've seen this error in this question: http://answers.unity3d.com/questions/58075/access-variable-in-a-class-from-another-script.html

I've tried it, but didn't work out too.

sem título.png (1.9 kB)
Comment
Add comment · Show 2
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 sethuraj · Oct 28, 2013 at 06:29 AM 0
Share

So you have two scripts,one on player and the other on the enemy I suppose and this is your enemy script

 var motor: Character;
 var playerDistance : Vector3;
  
 function Start () {
     motor = GetComponent(Character);
  
     playerDistance = motor.playerPosition - transform.position;
  
 }

try accessing the 'playerPosition' variable like this.

 void Start()
 {
   var playerpos=GameObject.Find("PlayerGameObjetName").GetComponent("PlayerScript").playerPosition; 
 
   playerDistance = playerpos - transform.position;
 }

Note - 'playerPosition' variable in player script should not be private and the player class should be $$anonymous$$onobehaviour

avatar image RafaelCN · Oct 28, 2013 at 07:30 AM 0
Share

No, my player script is more like this

 class CharacterInformation {
     //the position of the player.
     @System.NonSerialized
     var playerPosition : Vector3;
 }

The playerDistance does not exists on the player script.. I just wanna access the class "CharacterInformation" on the player script and access the var playerPosition which is a Vector3;

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by thef1chesser · Oct 28, 2013 at 09:46 AM

In most cases it just means you have to make your variables public. (protected also works in some cases, but than it has more restrictions)
If it still doesn't work add a using [namespace of other class] at the top and it will see the class.

Furthermore it depends on the design pattern you implemented. (or that you just wrote some spaghetti code...) More info on design patterns: http://www.oodesign.com/ With good design implementations you have the correct accessibility for the classes that need it.

Comment
Add comment · Show 4 · 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 RafaelCN · Oct 28, 2013 at 02:23 PM 0
Share

Well, i've just wrote some spaghetti code, as you saw, implementations I let for to do after, but the code isn't work out if I cannot access the variable inside my class...Summarizing, my problem is that I don't know access a variable inside a script from other GamObject

avatar image thef1chesser · Oct 29, 2013 at 08:34 AM 0
Share

step 1:
public var playerPosition : Vector3;

doesn't work? step 2: add:
import [insert namespace of gameObject script]

if ti still doesn't work can you add both scripts completely?

avatar image RafaelCN · Oct 29, 2013 at 03:40 PM 0
Share

@thef1chesser import? This is not the nomenclature of C#? Although I'm using JavaScript, but I'll change it later, the import namespace will doesn't work because we are not working with classes that inherit the $$anonymous$$onoBehaviour...

avatar image thef1chesser · Oct 30, 2013 at 07:43 AM 0
Share

because you use javascript and not C# it's import ins$$anonymous$$d of using.

and import is not the same as inheritage.

avatar image
0

Answer by fafase · Oct 30, 2013 at 08:15 AM

I think your problem is that your class does not inherit MonoBehaviour. Then GetComponent cannot work on it. Hence it does not find the component and return null.

 class CharacterInformation extends Monobehaviour{}
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.

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

Access class variables of selected object 1 Answer

Enum style variable 2 Answers

Editting a custom javascript class in the inspector 1 Answer

Access to a variable inside a C# class 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