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 Bitpocketer · May 01, 2015 at 05:00 PM · scritping

Why the state of the member array of the class isn't maintained outside the function it is initialized in?

I have an array of GameObject inside the class which is initialized in a method and given certain number of elements inside that method, however when that particular array is accessed outside the class it's initialized in, the array turns out totally empty. Another thing is the certain variables are public and assigned in inspector, despite of all that the console warns of unassigned public variables and causes mess. Attached pictures would illustrate what i mean.

[1]: /storage/temp/45587-prob2.jpg

prob1.jpg (31.2 kB)
prob2.jpg (74.7 kB)
Comment
Add comment · Show 2
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 Sessional · May 01, 2015 at 05:07 PM 0
Share

Can you tell us what lines it complains about unassigned public variables for? And what do you mean by causes mess?

avatar image steakpinball · May 01, 2015 at 07:34 PM 0
Share

I don't see the relation between the screenshots attached. Can you post the exact errors, as text? Use the 101010 button in the toolbar to post code.

2 Replies

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

Answer by Bitpocketer · May 01, 2015 at 11:13 PM

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 using System.Data;
 using Mono.Data.SqliteClient;
 using UnityEngine.UI;
 
 namespace Assets.Scripts
 {
     class sqlites : MonoBehaviour
     {
         
         String conn;
         SqliteDataAdapter myDataAdapter;
         SqliteCommand myCommand;
         DataSet myDataset;
         DataTable myDatatable;
         // Parent referencing transform of Question Containter
         public GameObject Parent=null;
         //Questionpanel a child panel of parent transform
         public GameObject QuestionPanel=null;
         GameObject[] a;
        // public GameObject button;
         //counter for questions category in database
         int obsession_categorycounter = 1;
         string Query;
         public GameObject next=null;
         GameObject[] quest=null;
         void Start()
         {
             
            
             FillQuestions(obsession_categorycounter);
           
            
         }
          
 
         public void FillQuestions(int counter)
         {
             //counter for categories
            
             conn = "URI=file:" + Application.dataPath + "\\NueralPunctures";
             Query = "Select  * From ObsessionsQuestions where ObsessionsQuestions.Type="+counter.ToString();
             myDataAdapter = new SqliteDataAdapter(Query, conn);
             myDataset = new DataSet();
             myDataAdapter.Fill(myDataset, "ObsessionsQuestions");
 
           
             int Total_Questions=myDataset.Tables["ObsessionsQuestions"].Rows.Count;
             Debug.Log(Total_Questions.ToString() + " Total questions");
             quest = new GameObject[Total_Questions];
             for (int i = 0; i < Total_Questions; i++)
             {
                 
                     quest[i] = (GameObject)Instantiate(QuestionPanel);
                     quest[i].transform.SetParent(Parent.transform, false);
                     quest[i].GetComponentInChildren<Text>().text = myDataset.Tables["ObsessionsQuestions"].Rows[i]["Question"].ToString();
                     Text txt = quest[i].transform.Search("Example");
                     txt.text = myDataset.Tables["ObsessionsQuestions"].Rows[i]["Example"].ToString();
                
             }
 
 
             GameObject mybutton = (GameObject)Instantiate(next);
             mybutton.transform.SetParent(Parent.transform, false);
 
         }
         public void test()
         {
             Debug.Log(quest.Length.ToString());//this report 0 elements in console
             for (int i =quest.Length; i >= 0;i--)
             {
                 Destroy(quest[i].gameObject); // and this throws indexoutofrangeexception;
             }
         }
     }
 }

the variables QuestionPanel,next,Parent are assigned in inspector, the quest array is initialized in FillQuestions() method. The test() method is triggered by button, when this test() method is triggered it throws indexoutofrangeException,and debug.log(quest.length.Tostring()) shows 0 elements in quest.

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
0

Answer by jmgek · May 01, 2015 at 09:25 PM

 ***Class Child***
  
 Public Child
     {
         public static int[] oneInt = {1,2,3};
     }
  
 
 ***Child2.cs***
 
     private void Main()
     {
          Child child1 = new Child();
          DebugLog(child1.oneInt[0]);
     }
  



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

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

22 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

Related Questions

How to make this script load next level after audio stops playing 3 Answers

instantiating prefabs from array 1 Answer

Player gets stuck between platforms!!! 1 Answer

Drag and drop a custom script into another script. 1 Answer

C# Fourier transform rhytm game implementation? 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