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 /
avatar image
0
Question by Reno-Fox · Oct 11, 2015 at 03:08 PM · scripting beginnerxmlinstantiate prefabxmlserializer

How do I spawn a random unit from my XML file?

Hey guys,

What I'm trying to do is:

Start the game, when the game starts you spawn 4 random champions from a database of hundreds onto a panel as it's a 2D game, and the champions will be more of a card form.

What I'm having problems with is how do I spawn 4 random champions with attributes from an XML file? I don't understand how to link the scripts to the xml.

Here's what I have so far:

This is the xml (the full list of champions is far longer):

 <?xml version="1.0" encoding="UTF-8"?>
 <ChampionCollection>
   <Champions>
     <Champion Name="Bob">
       <HP>5</HP>
       <MP>5</MP>
       <Attack>5</Attack>
       <Armor>2</Armor>
     </Champion>
     <Champion Name="Alex">
       <HP>10</HP>
       <MP>5</MP>
       <Attack>4</Attack>
       <Armor>1</Armor>
     </Champion>
     <Champion Name="Jess">
       <HP>20</HP>
       <MP>5</MP>
       <Attack>5</Attack>
       <Armor>0</Armor>
     </Champion>
   </Champions>
   </ChampionCollection>

The XmlSerializer script:

 using UnityEngine;
 using System.Collections;
 using System.Xml.Serialization;
 using System.IO;
 using System.Collections.Generic;
 
 [XmlRoot("ChampionCollection")]
 public class ChampionContainer {
 
     [XmlArray("Champions")]
     [XmlArrayItem("Champion")]
     public List<Champion> champions = new List<Champion>();
 
     public static ChampionContainer Load(string path)
     {
         TextAsset _xml = Resources.Load<TextAsset>(path);
 
         XmlSerializer serializer = new XmlSerializer(typeof(ChampionContainer));
 
         StringReader reader = new StringReader(_xml.text);
 
         ChampionContainer champions = serializer.Deserialize(reader) as ChampionContainer;
 
         reader.Close();
 
         return champions;
     }
 }



 

and another:

 using UnityEngine;
 using System.Collections;
 using System.Xml;
 using System.Xml.Serialization;
 
 public class Champion {
 
     [XmlAttribute("Name")]
     public string Name;
 
     [XmlAttribute("HP")]
     public float HP;
 
     [XmlAttribute("MP")]
     public float MP;
 
     [XmlAttribute("Attack")]
     public float Attack;
 
     [XmlAttribute("Armor")]
     public float Armor;
 }


Here's the spawn script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class SpawnScript : MonoBehaviour
 {
 
     public GameObject[] obj;
 
     // Use this for initialization
     void Start()
     {
         Spawn();
         Spawn();
         Spawn();
         Spawn();
     }
 
     void Spawn()
     {
         GameObject newCard = (GameObject)Instantiate
             (obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
         newCard.transform.parent = transform;
     }
 }


I'm not sure how I should really go about this, currently I have a prefab instantiated and I want to the prefab to pull from the XML, but I have a feeling that's not how it should be done, So I'm down for any suggestions to change it up. Heads up this is my 1st project so all I really know is basic code and what I've learned from watching a bunch of tutorials.

Any help would be greatly appreciated, I've been stuck on this part for a few weeks now and just really need some help so I can finally move on.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Confusion with process of XML setup 0 Answers

unity and xml question 1 Answer

Parse XML to Vector3 (and other objects you don't control) 0 Answers

Need Help With XML Reading Class Initialization 1 Answer

Create animation from script 0 Answers


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