Switching Scriptableobjects

Hello, i am trying to make a card game, so I made a lot of cards using ScriptableObjects.
It works nicely and card act as I want to when placed into script but I have a problem.
I want to change the cards(ScriptableObject) when i click a button or swipe template. Because there is one template to show a card and i want when i swipe that template switch that card to new one. Also i gave them some kind id and i want call them about that id by one by not from cards(ScriptableObject) name. Can you help me.

[CreateAssetMenu(fileName = "New Card", menuName = "Card")]
public class Card : ScriptableObject {

    public new string name;
    [TextArea(1,10)]
    public string description;

    public Sprite artwork;

    public int money;
    public int health;
    public int morale;
    public int eventid;
 
}


I want call and replace cards(ScriptableObject) by id

This is my card template like that

I don’t see any code like that above. It’s just a bunch of variables.

What part of your code isn’t doing what you want?

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

If you haven’t figured out what is malfunctioning yet, use this approach.

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

It’s a bit difficult to know how to help when I’m not sure how to script to switch your cards work, although based off what you described you could do this

// pass in your cards here
public List<card> cards = new List<card>();

int currentCard = 0;

Vector3 dropOff = Vector3.zero;

Vector3 OnScreen = Vector3.zero; // assign this the position where you want your on screen card to be

void Start()
{
       // assign the dropOff variable to some point off the game screen
       // I prefer to just move objects I'll use again later off the screen so you don't have to keep instansiating or finding them again
}
void Update()
{
      if (Input.GetKeyDown(keycode.whateverKeyYouWant))
      {
              cards[currentCard].transform.position = dropOff
              currentCard++;
              cards[currentCard].transform.position = OnScreen
      }
}

btw, this is a super simplified example of how to move the cards on and off the screen, it would defenitely need editing though as it’s way to simple to do what I presume is what you want, does this give you an idea of how to do what you were hoping?

1 Like

Is this not help something? Can’t we just write something else here than the problem? I just want get some script information. My game indeed working but i want add some feature. Let me explain you there is one “Card Template”(it shows your cards name, artwork and description when you insert your card or put in that template ) you can insert your cards(ScriptableObject) in that template. Also i can swipe that template or i can click button. I can do a lot of thing. But i need swich cards(ScriptableObject) when i click a button or swipe that template. Did i explain my problem clearly?

I do admit, it was difficult to know what you were trying to accomplish with your game due to lack of context, sure, we know you want to switch your cards out when you click a button, but that’s actually pretty vague, so it’s difficult to help you when were not even sure what you mean, adding pictures or being more specific of what you’re trying to do is a good way to give people the info they need to help

https://www.youtube.com/watch?v=Ac_fdVCrcZk

I got idea from that game everything done but cant switch events i will try your answer soon thank you by the way

ah, okay that helps a lot, so is the way the cards swapped out in the game trailer what you meant by swapping cards?

If what you’re trying to accomplish is what the game trailer did, I would personally create an animation for swiping the cards using Unity’s recorder functionality in the animator, I’d recomend you do more research on that yourself since that’s something you’d want to learn from a youtube tutorial rather than me trying to explain it

Yes when i swipe card left or right. It supposed to switch to new one. But it’s not randomly i gave them some id. I want call them by that id.


I think the best thing to do then is to take all your card objects, put them is a list, queue, hashset or whatever container you prefer, and then loop through the entire container and grabbing all your EventID’s and putting those in a seperate container, which you can access later, for example

using UnityEngine;

public class EventIDStore : MonoBehaviour
{
    // put all the cards you need in here through the inspector
    public CardClass[] cards;

    [HideInInspector]
    public static int[] allIDs;
    // this is static so you can access the values you stored between scenes, I assume that's what you want but if not you can just remove the static keyword

    private void Start()
    {
        for (int i = 0; i < cards.Length; i++)
        {
            allIDs[i] = cards[i].eventid;
        }
    }
}

by the way though, this script for some reason doesn’t allow me to access eventid from cards, I haven’t worked with scriptable objects before so I’m not sure why, so I’m just showing you it to explain my idea, but the script currently doesn’t work, so you’ll have to edit it if you want to use it.

1 Like

Thank you too much i solved my problem thanks to you.

1 Like

Hi, Im doing a very similar game mechanic (with the card switching).

What im doing is that each card has a unique button that you press and causes a different effect (for example: The player gains one life).

I also used scribtable objects to create all my cards but I haven’t found a way to add a field where I can make unique button script for each one within it.

Any ideas?

Steps to success:

  • don’t necro-post to unrelated random threads that happen to sound like your situation; it’s against forum rules and just muddies up the water. Odds are everybody above is long gone anyway.

  • When you post, make a new thread, and remember we can’t read your mind.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

If you have absolutely no idea what is happening in your project, you have to change that first. Here’s how:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

You must find a way to get the information you need in order to reason about what the problem is.