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 Optimus_L · Mar 06, 2012 at 07:47 PM · objectarraysobjects

Creating custom objects?

I'm looking to create a card trading game similar to Magic The Gathering.

I want to create a way to be able to easily make a card. So that in the Start() function I can simply initialise all my cards. Each card will need to have a unique ID for reference (Probably the objects name), to later be put into an array.

In my head, I know what I need to do, I'm just having an issue translating it into code as this is my first solo project.

After a bit of research I think it has to be done this way, and I just want it confirming by someone a bit more in the know.

So if I create a function..

Function Card () {

var name : String; var attack : int; var defence : int;

}

Then in Start(), I can initilise all my cards by using.... var c0001 = new Card(); c0001.name = "Red Creature"; c0001.attack = 1;

I can then create an array called Cards[], using the objects names c0001, c0002 and so on.

Have I got this right?

Comment
Add comment · Show 1
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 Jacob-Aldridge · Mar 06, 2012 at 08:00 PM 0
Share

I posted an asnwer for you Optimus_L, but it needs to be approved by a moderator before it is visible.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jacob-Aldridge · Mar 06, 2012 at 11:29 PM

I believe you are on the right track, but I think what you are talking about is creating a database of cards that you can reference easily and change easily.

You could create a Serialized class and then create a MonoBehaviour with an array of that class made public. You could then simply just modify the GameObject from within Unity and add/remove cards as you need. If you wanted to get fancy, you could create an Editor class for Unity and have a decent interface for creating cards in your CardManager.

 using UnityEngine;
 using System.Collections;
 
 [Serializeable]
 public class Card
 {
    public String name = String.Empty;
    public Int32 damage = 0;
    public Int32 defense = 0;
 }

 public class CardManager : MonoBehaviour
 {
    public List<Card> cards = new List<Card>();
 
    public void Start()
    {
    }

    public void Update()
    {
    }
 }
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 Kiloblargh · Mar 06, 2012 at 08:44 PM

I don't know why you think Card() should be a function. I also don't think you'd be creating new cards in the game, you'd have them all pre-designed.

So I would make each card a prefab, with a mesh and material for its appearance, and attach a "cardScript" on it that holds its stats.

 var theDeck : GameObject[];

 var myCards : List.<cardScript>();
 var yourCards : List.<cardScript>();

 function DealCards (howMany : int)
 {
 
 }

 function CompareCards (first : cardScript, second : cardScript)
 {
 
 }

There's a lot more to this sort of game that you're going to run into- how you are going to save a player's cards in a way that isn't easily cheatable, how to draw a random card with a the right probability based on its rarity, etc.

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 Optimus_L · Mar 06, 2012 at 09:57 PM 0
Share

Actually that was my first solution, but I got confused thinking if I created a single script called CardInfo and attached it to a card I thought it would change all the values of all the cards but after trying it turns out it works fine. ...I'm still learning =/

Thanks for the help

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the size of an array of object? 2 Answers

ObjectPicker Source Code? 0 Answers

Can't get more than one object. 1 Answer

Object carry in game 1 Answer

What is the maximum amount of objects that can be stored in an Array or List without causing any error? 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