Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Nov 11, 2016 at 09:41 AM by RangerDog for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RangerDog · Nov 03, 2016 at 09:31 AM · variablescope

Weird Variable Scrope Problem

So basically in start, I am assigning DayNightController

     void Start()
     {
         DayNightController dayNightScript = DayNightCycle.GetComponent<DayNightController>();
     }

Then, when i try to access it anywhere else.

 dayNightScript.daySpeedMultiplier = 0.05f;

It doesn't recognize dayNightScript, doesnt matter where i put it, not sure why it does this.... (Its probably something simple, just cant come up with it XD)

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by saschandroid · Nov 03, 2016 at 10:10 AM

"Then, when i try to access it anywhere else." Do you mean trying to access it outside Start()? That's not possible, it's only known in that method. You have to declare it outside:

 public class YourClass : MonoBehaviour
 {
     DayNightController dayNightScript;
    void Start()
     {
         dayNightScript = DayNightCycle.GetComponent<DayNightController>();
     }
 }
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 RangerDog · Nov 03, 2016 at 10:20 AM 0
Share

Nop, as in i am trying to acces it a if statement in the start.

avatar image a7BiT-psycho RangerDog · Nov 03, 2016 at 10:34 AM 0
Share

We need to see more of your script, not just the declaring and assignment. Can you post the entire Start function/or try to cut it down to the important parts? Also what is the error message? Is it a NullReferenceException during runtime or are you getting a compiler error?

avatar image RangerDog a7BiT-psycho · Nov 03, 2016 at 11:11 AM 0
Share

Nothing of that, it simply way doesn't recognize the variable in Visual Studio already.

GameObject Declaration:

 public GameObject DayNightCycle;

Start Function

 void Start()
     public virtual void OnGripped(ClickedEventArgs e)
     {
         if (Gripped != null)
             Gripped(this, e);
 
         DayNightController DayNightScript = DayNightCycle.GetComponent<DayNightController>();
         DayNightScript.daySpeed$$anonymous$$ultiplier = 0.5f;
     }
 
     public virtual void OnUngripped(ClickedEventArgs e)
     {
         if (Ungripped != null)
             Ungripped(this, e);
 
         DayNightController DayNightScript = DayNightCycle.GetComponent<DayNightController>();
         DayNightScript.daySpeed$$anonymous$$ultiplier = 0.05f;
     }


Small Edit: Right now i am declaring it in each function, this way it works.

Show more comments
avatar image RangerDog · Nov 04, 2016 at 10:34 AM 0
Share

@a7BiT-psycho

Well, the first part i am not 100% sure, it's a script from s$$anonymous$$mVR and it seems to be working so yeah. Anyways, just to be sure i tested it on another script, and it seems to work there, so yeah i have to figure out how to do it then, considering its a API. Anyways, thanks for the help!

Follow this Question

Answers Answers and Comments

61 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 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 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

Accessing variable from another class gives initial value 1 Answer

Accessing EditorWindow variables from game code (only on editor play) is not possible? 1 Answer

How to approach individual variables for cooking game 1 Answer

Get A Variable from another Script in Unity iPhone 2 Answers

Can I declare a variable inside a function which has instance scope? 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