I want to create a whole class of spells for a card game. I want to store a huge bank of spells in one script, and call upon them throughout the game. In this instance I want to create a spell book, where I can create/browse my decks
this is the start of the bank of spells script.
using UnityEngine;
using System.Collections;
public class SBSpells : MonoBehaviour {
public class spell {
public int mana;
public string typeClass;
public bool unlocked;
public spell(int mana, string typeClass, bool unlocked) {
}
public spell Dragon1 = new spell(2, “DragonTamer”, true);
public spell Dragon2 = new spell(3, “DragonTamer”, true);
void Start () {
}
void Update () {
}
}
}
I want to in another script create tabs/box/buttons within the screen of the different cards which you can click, to start building a deck all within the GUI. But I don’t know how to call upon the variables. I have tried researching stuff but it is hard to understand as there seem to be no clear answer. So looking for some help/advice for my problem.
Ah is a list better then classes? I was having a lot of debate with my friends with which is better. We are still new to c#. So would a list make it easier? and if so how would I write it to access a variable from the list?
A list in the inspector is easy to access and manage. If you want to access the list, create a reference to the object holdign the list using GetComponent.
But that is the problem, this bank of cards is just a virtual storage which allows us to access the information throughout the game. It isn’t attached to any game objects. It just exists and things exists from its information
Hm. Okay these were some ideas we were knocking around before. The problem we have we just don’t know which would be the best in our scenario? I think I want to avoid using a game object. But does it even matter if you use one? I am unsure if I am being honest. Any tips?
I made a empty game object and called it GameController and put my start up scripts and things I need to reference like spells … and just use find object and get component
you can make a GameObject go to multiple scene’s with DontDestroyOnLoad(myGameObject); … It wont show up in the editor on other scenes but using find object in code will find it then.
Ah okay. And would that probably be the cleanest way to do it?
Also I’m getting an error message. Unsure why, any help would be great. Confused what the problem is:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SBSpells : MonoBehaviour {
public class Spell {
public int mana;
public string typeClass;
public bool unlocked;
}
public Spell(int mana, string typeClass, bool unlocked) {
}
public Spell Dragon1 = new Spell (2, “DragonTamer”, true);
public Spell Dragon2 = new Spell (3, “DragonTamer”, true);
public static class Spellbook {
public static List Spells = new List ();
Spells.Add (Spell(Dragon1));
Spells.Add (Spell(Dragon2));
}
Spells.Add (Spell(Dragon1)); would just be Spells.Add (Dragon1); I think
Also personally I don’t like to use static if I don’t have to.
I would make the Gameobject you have spells on have its own tag and use
and
Im not sure why static is bad … I just remember my programming 101 class that’s what the professor said…
That was a while back in c++. Maybe things Changed idk
Also you can Serializable the spell if your design allows then just have a public List = new List(); on the game object… and when you click that game object in the editor you can add spells right in the inspector instead of in code… sometimes it makes it a bit easier to look back and modify a spell
Something I use for commands in my game
using UnityEngine;
using System;
[Serializable]
public class Command
{
public int ID;
public string name;
public Sprite Icon;
public GameObject prefab;
public ActiveCommandCard activeCommand;
}
then on the game object I have a script that is
using UnityEngine;
using System.Collections;
public class Commands :MonoBehaviour
{
public List<Command> = new List<Command>();
}
then I can add / modify all my commands right in the inspector
Thank you so much for all the help. I’m going to give both options a spin and just see what happens. I need to learn lots, so thank you for all the help. I may be back with more questions, but hopefully not for a while!
When I type in the GameObject script it tell me I have an unexpected symbol ‘=’ in class, struct, or interface member declaration. And I have no idea why that would be happening. I copied your code word for word.
Yeah I thought that was the problem. But I get an error after that. This is my adapted code.
using UnityEngine;
using System;
public class Spell {
public int ID;
public string name;
public Sprite Icon;
public GameObject prefab;
}
then the gameobject script:
using UnityEngine;
using System.Collections;
public class SpellList :MonoBehaviour
{
public List SpellList = new List();
}
and my error is = error CS0542: ‘SpellList.SpellList’: member names cannot be the same as their enclosing type
edit: just fixed it but now I have "The tpy eor namescape name ‘List’1’ could not be found. are you missing a using directive or an assembly reference? FIXED. but I had to use System.Collections.Generic; but I am not getting anything in the editor like you mentioned.