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 John_Sherer · Apr 07, 2014 at 01:52 PM · variablefogscope

Accessing variable from another class gives initial value

Hi, my problem is fairly simple: whenever I access a variable from another script, I only ever receive the value that I originally assigned to that variable. Here's my code:

public class PickupScript : MonoBehaviour {

 public int progress = 0;
 private CreatureNav script;

 void Start () {
     script = new CreatureNav ();
 }
 
 void Update () {
     if(condition){
                 progress=1;
             }
 }
 public int getProgress(){
     return progress;
 }

}

public class CreatureNav : MonoBehaviour {

 private PickupScript script;

 void Start () {
     script = new PickupScript ();
 }
 
 public void Update () {
     if (script.getProgress () == 1) {
         RenderSettings.fog = true;
     }
 }

}

The most anoying bit is that I don't get any kind of error message, it just doesn't work! My intent is to create fog when "condition" is met, but nothing happens. I've done some bug checks, and "getProgress()" always returns 0. Accessing int "progress" directly using "script.progress" also turns up as 0. Can someone please explain what's going on?!

Comment
Add comment · Show 4
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 GenuiTix · Apr 07, 2014 at 02:18 PM 0
Share

As far I know you can`t use --new-- word with $$anonymous$$onoBehaviour like in here

 new PickupScript ();


The other thing is condition might be not met. Try

 void Update () {
     progress=1;
 }

to test interace.

and

 void Update () {
     if(condition){
         Debug.Log("condition met");
     }
 }

to test statement.

avatar image John_Sherer · Apr 07, 2014 at 02:44 PM 0
Share

I have tested and confirmed that the condition works perfectly and is met when it is intended.

avatar image Dep · Apr 07, 2014 at 02:52 PM 0
Share

Your problem is probably that Unity doesn't call the methods on the PickupScript because it isn't assigned to a gameobject.

avatar image John_Sherer · Apr 08, 2014 at 01:46 PM 0
Share

I have PickupScript attached to a gameobject as a script component.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CodeElemental · Apr 07, 2014 at 02:57 PM

The problem is that the script = new PickupScript (); Method instantiates a new instance of the script, unrelated to the one attached on the gameobject (which I assume is the one you want to use). So instead you need to change the code : from

 void Start () {
     script = new PickupScript ();
 }

to

 void Start () {
     script = GetComponent<PickupScript> ();
 }
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 Dep · Apr 07, 2014 at 02:59 PM 0
Share

But if it has to be created at runtime you can simply use AddComponent

avatar image John_Sherer · Apr 08, 2014 at 01:45 PM 0
Share

Hmm.. what you're saying makes sense, but the console is reporting a NullReferenceExeption. Here's the new code: public class CreatureNav : $$anonymous$$onoBehaviour {

     private PickupScript script;
 
     void Start () {
             script = GetComponent <PickupScript> ();
     }
     
     // Update is called once per frame
     public void Update () {
         if (script.progress == 1) {
             RenderSettings.fog = true;
         }
     }
 }

It refers to the first line of the "if" statement, and says, "Object reference not set to an instance of an object". In the other script, I have the line of code:

public int progress = 0;

So I think that it's talking about the script variable.

If it helps, my scripts are attached to two different objects. Can you help me again please?

avatar image CodeElemental · Apr 08, 2014 at 02:53 PM 0
Share

Then you need to get reference to the game object the script is attached to, first :

 void Start () {
     GameObject go = GameObject.Find("Object name"); // Replace "Object name" with the name of your object.
     script = go.GetComponent<PickupScript> ();
 }
avatar image John_Sherer · Apr 08, 2014 at 04:08 PM 0
Share

I'm still getting the NullReferenceExeption. The new start statement is:

 void Start () {
         GameObject go = GameObject.Find("$$anonymous$$ain Camera");
         script = go.GetComponent<PickupScript> ();
     }

I'm using a first person controller, so the camera with the script is parented under another gameobject. Does that change anything? I'm sorry that I didn't tell this to you to start with, but you're being a great help! Thanks, and hope you still want to help me.

avatar image John_Sherer · Apr 08, 2014 at 06:01 PM 0
Share

Alright, I was able to find a solution. Ins$$anonymous$$d of searching for a specific object, I can just specify it in the inspector if I set it as a public variable. Your information was incredibly valuable, and I would probably still be stuck if it wasn't for you. Thank you so much!

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

23 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 avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Scripts accessing one another (JS) 0 Answers

Help with my code?(NullReferenceException)[CLOSED] 2 Answers

Gun Script Help 2 Answers

Public variable in script different for every game object. 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