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 maggot · Nov 25, 2011 at 12:37 PM · c#assetsloadingrunonce

Do I have to tell unity to load assets only once with cSharp script

I have this cSharp code which runs fine, but I want to add a boolean run_once control to my code as it is part of a class that scans input and is called by Update

         Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
         GameObject playerMissile001 = initializeModels.playerMissile001;
         GameObject playerMissile002 = initializeModels.playerMissile002;
         GameObject currentMissile = (GameObject)playerMissile001;

If I enclose the code with a boolean run_once set to false in a containing class as such :

 if(!run_once)
 {
        Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
         GameObject playerMissile001 = initializeModels.playerMissile001;
         GameObject playerMissile002 = initializeModels.playerMissile002;
         GameObject currentMissile = (GameObject)playerMissile001;
         run_once = true;
 }

I get errors such as :

Assets/Scripts/player001controls.cs(161,42): error CS0135: `playerMissile001' conflicts with a declaration in a child block

So, how do I get around this, and do I have to? Perhaps unity runs those lines of code only once as they refer to loading assets.

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

4 Replies

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

Answer by maggot · Dec 24, 2011 at 04:39 PM

No, I can use static variables instead.

With static variables I don't need to create an instance of InitializeModels. Because of this I removed this line of code that instantiated InitializeModels :

 InitializeModels initializeModels = new InitializeModels(); 

I added the word static after public or private to all the variables inside the class InitializeModels()

I removed this code :

  if(!run_once) // Removed this code block
  {
    Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
    GameObject playerMissile001 = initializeModels.playerMissile001;
    GameObject playerMissile002 = initializeModels.playerMissile002;
    GameObject currentMissile = (GameObject)playerMissile001;
    run_once = true;
 }

I removed this code declaring run_once as it isn't called anymore :

     bool run_once = false;

I replaced all occurences of initializeModels. with InitializeModels

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

Answer by ks13 · Nov 25, 2011 at 01:34 PM

HI, can't really see what's the problem with only that little part of your code but "conflicts with a declaration in a child block" means you have another declaration of "playerMissile001" in another block when you should have only one. Use CTRL+F to find where you're declaring the objects and remove those not needed.

Comment
Add comment · Show 1 · 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 maggot · Nov 25, 2011 at 01:55 PM 0
Share

I think the problem has to do with scope limits. Perhaps if I enclose the code in an if statement, it becomes inaccessible outside the If statement.

avatar image
1

Answer by ptdnet · Nov 25, 2011 at 01:56 PM

For what it's worth, those objects you are declaring inside the if { } are going to vanish immediately after you exit the code block. I have a feeling you mean to do this:

 GameObject playerMissile001 = null;
 
 if (!run_once) {
     playerMissile001 = initializeModels.playerMissile001;
     run_once = true;
 }
Comment
Add comment · Show 1 · 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 maggot · Nov 25, 2011 at 03:41 PM 0
Share

O$$anonymous$$, that's working, although I had to make a few changes to my code. I added all relevant GameObject types to my initialize$$anonymous$$odels class with get{} and set{}, assigned all GameObject types to null in the declaration part of Player001Controls, and edited one line of code which gave an error.

avatar image
1

Answer by ks13 · Nov 25, 2011 at 04:02 PM

Which means you already declared it before... Try to remove "GameObject" before the local variables in the "if" block like :


    if(!runonce)
    {
           Transform player001ShotSpawnPoint001 = initializeModels.player001ShotSpawnPoint001;
           playerMissile001 = initializeModels.playerMissile001;
           playerMissile002 = initializeModels.playerMissile002;
           currentMissile = (GameObject)playerMissile001;
           runonce = true;
    }
    

Comment
Add comment · Show 1 · 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 maggot · Nov 25, 2011 at 04:09 PM 0
Share

Well I got it working with those GameObject type declarations in the If block before, but I removed them now and it still works fine.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Generating and scrolling assets 0 Answers

C# PlayerPrefs not Work 2 Answers

how to preload assets with progress bar 1 Answer

LoadAssetAtPath() returning null IN EDITOR 1 Answer

loading prefabs 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