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 Rgalaxy · Dec 12, 2013 at 07:01 PM · variableaccessinganother

Accessing variable from another script

Holla !

im trying to access a variable from another script, but i just couldn't get it done.. no matter how i do, the error is always Object reference not set to an instance of an object

so,, here's my code

chgChar.js

 var isMale = false;
 var isFemale = false;
 public var selectedplayer : int = 0;
 static var player_gender : String = "Female";

 function OnMouseUp() {
 if(isMale == true) {
     player_gender="Male";
     print(player_gender);
     GameObject.Find("Boy").transform.position.x = -5;
     GameObject.Find("Girl").transform.position.x = -25;
     selectedplayer = 1;
     //PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
 }
 if(isFemale == true) {
     player_gender="Female";
     print(player_gender);
     GameObject.Find("Boy").transform.position.x = -25;
     GameObject.Find("Girl").transform.position.x = -5;
     selectedplayer = 2;
     //PlayerPrefs.SetInt("selectedplayer", (selectedplayer));
 }

}

charSpawn.js

 var female : GameObject;
 var male : GameObject;
 var targetscript : chgChar;
 var selected : int;
 
 
 function Start () {
     targetscript=GetComponent.<chgChar>();
     selected = targetscript.selectedplayer;

     if(selected==1){
         Instantiate(male,Vector3(0,0,0),Quaternion.identity);
     }
     else if(selected==2){
         Instantiate(female,Vector3(0,0,0),Quaternion.identity);
     }
 }

anyone know what my doing was wrong? btw the error is on this line

 selected = targetscript.selectedplayer;
Comment
Add comment · Show 7
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 Rgalaxy · Dec 12, 2013 at 08:48 PM 0
Share

could anybody helps?

avatar image fafase · Dec 13, 2013 at 05:57 AM 0
Share

Are they both on same object?

avatar image Peter G · Dec 13, 2013 at 06:22 AM 0
Share

A few suggestions:

This line:

  if(chgChar.selectedplayer==1){

should probably be:

  if(selected==1) {

You are currently trying to access an instance variable through a class. Same thing goes for the if else statement underneath it.

Also, make sure there is chgChar attached to the same object as the charSpawn, and you should use the generic version wherever you can:

 targetscript = GetComponent.<chgChar>();

for performance and type-safety's sake.

avatar image Rgalaxy · Dec 13, 2013 at 06:48 AM 0
Share

thanks @Peter G yeah i have change it now.. but i dont udnerstand, u mean by attached to the same object, that 2 scripts must come from same object? i must atttach it to same 3d object?

but the chgChar is one char selection scene, while the charSpawn is in the game scene where i will spawn the player..

avatar image Vandarthul · Dec 13, 2013 at 06:48 AM 0
Share

Probbably your chgChar.js and charSpawn.js is not on the same object. Therefore first you will have to find on which object your chgChar.js attached to. So try to correct this line first:

lets say that your chgChar.js is on an object named Character and your Character's tag is Char then you can find the object with 2 methods:

 targetscript=GameObject.Find("Character").GetComponent<chgChar>();
 //OR
 targetscript=GameObject.FindGameObjectWithTag("Char").GetComponent<chcChar>();

I have provided C# codes since I'm not familiar with JavaScript. But you should get the idea behind it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Dec 13, 2013 at 07:29 AM

Need '()' at the end of GetComponent:

 targetscript=GameObject.Find("Character").GetComponent<chcChar>();

The Javascript version of get component:

 targetscript=GameObject.Find("Character").GetComponent(chcChar);
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 Rgalaxy · Dec 13, 2013 at 08:39 PM 0
Share

@robertbu hi robert ! i've tried to change it to GameObject.Find("genderBoy").GetComponent(chgChar); but the problem is still the same,

 targetscript=GameObject.Find("genderBoy").GetComponent(chgChar);

NullReferenceException: Object reference not set to an instance of an object charSpawn.$$anonymous$$ain () (at Assets/testing script/charSpawn.js:8)

avatar image robertbu · Dec 13, 2013 at 08:48 PM 0
Share

Spelling must be exact. 'genderBoy' != 'GenderBoy' != 'Gender Boy'... And the 'chgChar' component must be on the game object with the name you specify. If you have more than one game object with the name 'genderBoy', check to make sure they all have the 'chgChar' script attached.

avatar image Rgalaxy · Dec 13, 2013 at 08:55 PM 0
Share

so, basically, i have 2 object too choose wether the char is boy or girl, its 2 different object if i click genderBoy, the char shown will become boy, and if i click genderGirl, the char shown will become girl. and this 2 object are in charSelection scene.

and i have 1 scene called GameWorld, where i want to spawn my boy/girl there, which why i put charspawn.js on here, at GameWorld scene..

that's why it will be so weird for me if i should find GameObject where chgChar.js is on.. becase it's on different scene..

i know it is confusing.. u want me to sent u my project ins$$anonymous$$d?

avatar image robertbu · Dec 13, 2013 at 10:18 PM 0
Share

Pretty confusing situation. Are you sure that 'genderBoy' exists at the time you are executing this code? Try executing this just before you do the get component call:

 var go = GameObject.Find("genderBoy");
 Debug.Log(go); 
avatar image Rgalaxy · Dec 14, 2013 at 12:44 AM 0
Share

i dont think it will come up with anything since they are on different scene?

but i did tested it and

null UnityEngine.Debug:Log(Object)

that's what the debug says..

emm, i dont think u quite get what i want.. basically i want to spawn the character, which i choose on another scene.. so that's why the charSpawn is on another scene..

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

19 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

Related Questions

Is it possible to change a variable, into a script not assigned to any game object? 3 Answers

Can't understand generic getComponent for Js 1 Answer

Global Varible Problem 2 Answers

Can't access variable from another script. 1 Answer

Accessing A Variable From Another Script 4 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