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 simpson457 · Nov 11, 2012 at 12:36 PM · variablespass

Can't pass variables between 2 scripts

hi,

I have 2 scripts, a renderItem script assigned to 10 item gameobjects(locations) out of which only 1 will spawn(render) and a globalVariables script assigned to an empty game object in the scene to select which location will be spawned.

when I run the following scripts, nothing is generated in the scene. I found out that the problem is that renderItem.js for each item cannot see any variables from the globalVariabes script. Any ideas on what I am doing wrong?

I have used the same type of setup for OnMouseDown() functions in the past and it works, why does it not work with Start() or Update()?

Thanks in advance!


renderItem.js:

var KeyObj:GameObject;// model to be rendered

var keyPosition:int;//manually set when placed in scene(1,2,3...etc)

function Start()

{

var scriptt: globalVariables = GetComponent(globalVariables);

KeyObj.renderer.enabled=false;

if((script.keyPos[keyPosition]))

  KeyObj.renderer.enabled=true;

}


globalVariables.js

static var keyPos:boolean[];//key spawn poing

keyPos= new boolean[10];//10 locations to choose from

function Awake(){

//following values will be randomly assigned later on

keyPos[3]=false;

keyPos[2]=true;//spawn at location 2

keyPos[1]=false;

keyPos[0]=false; }

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 fafase · Nov 11, 2012 at 12:59 PM 0
Share

You can see a long explanation on http://unitygems.com/. You can find how to access variables from other scripts and an explanation on static variables in the memory management section.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Nov 11, 2012 at 12:45 PM

You can access static variables directly using the script name (class name, actually) directly like this:

   ...
   if((globalVariables.keyPos[keyPosition]))
      KeyObj.renderer.enabled=true;
   ...

NOTE: Your original code would work only if globalVariables.js were attached to the same object as renderItems.js.

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 simpson457 · Nov 11, 2012 at 01:13 PM 0
Share

Thanks for answering! I didn't know that. I tried it but still no luck though. Also, i noticed that I get a log error:"Object reference not set to an instance of an object" for the above if statement, any clues?

Thanks again!

avatar image fafase · Nov 11, 2012 at 01:58 PM 0
Share

Do you have anything in the $$anonymous$$eyObj slot of the Inspector or does it say None(GameObject)?

avatar image simpson457 · Nov 11, 2012 at 02:54 PM 0
Share

yes I have the model prefab(object to be rendered)

avatar image aldonaletto · Nov 12, 2012 at 02:46 AM 0
Share

You usually get errors when trying to modify prefab properties by script - do it in the instantiated object ins$$anonymous$$d. You could instantiate the $$anonymous$$eyObj first, then modify the clone like this:

 var kObj: GameObject = Instantiate($$anonymous$$eyObj, ...); // get a ref to the clone
 if (globalVariables.keyPos[keyPosition])
   kObj.renderer.enabled=true; // set the clone renderer
 ...

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

11 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

Related Questions

What is the best way to share data across child classes? 1 Answer

Public variables not updating? 1 Answer

cleanup variables on editor BUG? 1 Answer

How to fix unstable boolean values? [possible bug] 1 Answer

Accesing variable previously declared? 2 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