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 ardizzle · Mar 30, 2013 at 10:15 PM · javascriptvariables

A variable im trying to pull from another script is reseting when it shouldn't?

I have two scripts. The buttons script is trying to pull numberOfProjects variable, from the projectList script, every time the game object its attached to is cloned. Thus making a list of objects i can later turn into buttons. The problem i have having is that numberOfProjects keeps displaying 0 no matter what. And what makes it weird is that when i have it print the number its prints it at one first and then zero immediately afterwards. So here are my two scripts

projectList

 // Project List
 
 #pragma strict
 
 // Inspector Variables
 var numberOfProjects         : int         = 1;                        // Number of projects you have
 var projectSelecterXPos      : float;                                // X postions for all project selecter buttons
 var projectSelecterYStartPos : float;                                // the starting y position that the NewProject function is based on
 var projectSelecterGap         : float;                                // the gap between your project selecter buttons
 var projectNameList          : String      = "Project Name List ";    // name of the box that hold your projects
 var projectSelecterButton      : Transform;                                // needed to make copys of text box
 
 // Private Variables
 private var projectManagerArray            = Array(numberOfProjects); // may be needed later. not used yet
 private var numberOfProjectsHolder        : int         = 0;           // needed to hold a number for NewProject function    
 function Start () 
 {
     transform.position = Vector3(projectSelecterXPos,projectSelecterYStartPos,10);    
 }
 
 function Update () 
 {
     NewProject();
 }
 
 function NewProject()
 {
     if(numberOfProjects>numberOfProjectsHolder)
     { 
         var projectSelecterYPos     = Array(numberOfProjects);
         var x = projectSelecterYPos.length;
         Instantiate(projectSelecterButton,Vector3(projectSelecterXPos,projectSelecterYStartPos - x * projectSelecterGap, 10), Quaternion.identity);
         numberOfProjectsHolder++;
     }
 }

Buttons

 // Project Buttons
 
 #pragma strict
 
 var projectNumber : int;
 private var projectListScript : ProjectList;
      
 function Start ()
 {
 projectListScript = GetComponent(ProjectList);
 projectNumber = projectListScript.numberOfProjects;
      
 }
 
 function Update () 
 {
 print(projectNumber);
 }

This is what the console displays in this order.

1 UnityEngine.MonoBehaviour:print(Object) Project buttons:Update() (at Assets/Project buttons.js:17)

NullReferenceException: Object reference not set to an instance of an object Project buttons.Start () (at Assets/Project buttons.js:11)

0 UnityEngine.MonoBehaviour:print(Object) Project buttons:Update() (at Assets/Project buttons.js:17)

Comment
Add comment · Show 13
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 Unitraxx · Mar 30, 2013 at 10:34 PM 0
Share

Is the ProjectList script properly connected to the Buttons script? It's a compiler error, so it's still an old version of the code that you run.

avatar image ardizzle · Mar 30, 2013 at 11:18 PM 0
Share

What do you mean properly connected. and after a little bit of experimenting i noticed that if i change number of projects it still prints the number one along with the number i changed it to.

avatar image Unitraxx · Mar 30, 2013 at 11:21 PM 0
Share

That's probably because in the Start() method you reinitalise the variable. (line 19) Try deleting that, and tell us what output you get.

avatar image ardizzle · Mar 30, 2013 at 11:27 PM 0
Share

then i just get the error listed above. no numbers

avatar image sparkzbarca · Mar 30, 2013 at 11:30 PM 0
Share

he means that the code is saying

getcomponent(projectlist)

error the component project list is not a component of the game object the button script is attached to

so for this code to work

there should be a game object and the script projectlist should be attached to it. as well the script buttons.

see that null reference exception.

what it's saying is that when it tries to resolve

projectscript.numberofprojects it comes back null

since numberofprojects is a variable with a default value in the projectscipt you have shwon

the only possibility is that getcomponent is returning null

if you were to do

if( ! getcomponent(projectscript){ debug.log("there is no attached project script"); }

it would print that i'm betting. Because the component isn't attached.

So you need to make sure both scripts are attached to the same object.

the compiler doesnt check as your typing because you may be adding or subtracting components in the code. so this error wont pop up until run-time.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

13 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Error BCE0005 in Javascript 1 Answer

How do i get attributes of another object? 1 Answer

Having a script variable update between two objects 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