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 vishnu gb · Mar 03, 2015 at 09:07 AM · instantiatecubexmlread

instantiate a cube at a position by read from xml file

hello, While reading from the xml file,instantiating of start and dest is not visible(it shows up when z cooedinate value changed to -2) while without xml reading it works fine(for openf == 0).Iam really not able to figure out what is the problem.Please help

void Start()

 {
     if (PlayerPrefs.GetInt("openf") == 0)
     {
         size.x = PlayerPrefs.GetInt("size_l");
         size.y = PlayerPrefs.GetInt("size_b");
         parent_nodes = new nodes[(int)size.x * (int)size.y];
         grid_arr = new Transform[(int)size.x, (int)size.y];//array to refference to grids created
         for (int x = 0; x < size.x; x++)//creating grids
         {
             for (int z = 0; z < size.y; z++)
             {
                 Transform newcel;
                 newcel = (Transform)Instantiate(cellprefab, new Vector3(x, z, 0), Quaternion.identity);
                 newcel.name = (newcel.position.x.ToString() + newcel.position.y.ToString());
                 newcel.parent = transform;
                 grid_arr[x, z] = newcel;
             }
         }
         creategrid();
     }
     else 
     {

         string xml = PlayerPrefs.GetString("xmlfile");
         int x = 0, y = 0;
         file_path = PlayerPrefs.GetString("file");
         if (file_path != "")
         {
             XmlDocument xmlDoc = new XmlDocument();
             xmlDoc.LoadXml(xml);
             XmlElement root = xmlDoc.DocumentElement;
             if (root.HasAttributes)
             {
                 int.TryParse(root.GetAttribute("size_x"), out x);
                 int.TryParse(root.GetAttribute("size_y"), out y);
                 size.x = x;
                 size.y = y;
                 parent_nodes = new nodes[(int)size.x * (int)size.y];
                 grid_arr = new Transform[(int)size.x, (int)size.y];//array to refference to grids created
             }
             
             XmlNodeList grds = xmlDoc.GetElementsByTagName("Grid");
             bool f = false;
             foreach (XmlNode nd in grds)
             {
                 XmlElement elm = (XmlElement)nd;
                 if (elm.GetElementsByTagName("name")[0].InnerText == "block")
                 {
                     int.TryParse(elm.GetElementsByTagName("X")[0].InnerText, out x);
                     int.TryParse(elm.GetElementsByTagName("Y")[0].InnerText, out y);
                     b = (Transform)Instantiate(block, new Vector3(x, y, -1), Quaternion.identity);
                     b.parent = transform;
                     b.name = "block";
                     grid_arr[x, y] = b;
                 }
                 else
                 {
                     int.TryParse(elm.GetElementsByTagName("X")[0].InnerText, out x);
                     int.TryParse(elm.GetElementsByTagName("Y")[0].InnerText, out y);
                     b = (Transform)Instantiate(cellprefab, new Vector3(x, y, -1), Quaternion.identity);
                     b.parent = transform;
                     b.name = x.ToString()+y.ToString();
                     grid_arr[x, y] = b;
                 }
             }
 
         }
         else
         {
             MessageBox.Show("!!!Corrupted XML file \n \t or\n Invalid XML file...");
         }
         creategrid();
     }
 }








void creategrid()

 {
     Camera.main.transform.position = new Vector3(grid_arr[(int)(size.x / 2), (int)(size.y / 2)].position.x, grid_arr[(int)(size.x / 2), (int)(size.y / 2)].position.y, -5);//scaling to the view
     /////////////////////////////////////////Set camera view///////////////////////////////////////////////
     bool f = false;
     for (int i = 0; i < size.x; i++)//creating grids
     {
         for (int j = 0; j < size.y; j++)
         {
             if (grid_arr[i, j].name != "block")
             {
                 if (f == false)
                 {
                     start = (Transform)Instantiate(start, new Vector3(i, j, -1), Quaternion.identity);
                     dest = (Transform)Instantiate(dest, new Vector3(i, j, -1), Quaternion.identity);
                     f = true;
                     break;
                 }
             }
         }
         if (f == true)
             break;
     }
     
     if (size.x > size.y)
     {
         Camera.main.orthographicSize = (size.x / 2) + 3f;//projecting the camera view to the game
     }
     else if (size.x < size.y)
     {
         Camera.main.orthographicSize = (size.y / 2) + 3f;//projecting the camera view to the game
     }
     else
     {
         Camera.main.orthographicSize = (size.x / 2) + 3f;//projecting the camera view to the game
     }

 }
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Reading XML file 0 Answers

Place cubes? problem with my script :S 1 Answer

How do I read XML on iPhone? 3 Answers

How to instantiate a custom sized object? 1 Answer

Reading XML files that have a full DTD 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