• 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 renomabum · Jan 08, 2019 at 08:28 AM · prefab-instance

how do i instantiate prefab and load its data from scriptable object .,i want to instantiate prefab and load its asset right away.

hello . i am new to unity and trying to create a TCG kind of game. the problem that i am facing now is to instantiate my prefab and load its asset at single click.

alt text

this is my script attached to the card prefab

 public class CardData : MonoBehaviour
 {
     public SOcard socard; //scriptableobject script
 
     public string TitleData;
     public Image CardImageNormalData;
     public int PowerData;
     public int ResistanceData;
     public Sprite CardImageLargeData;
 
     void Start()
     {
         
     }
     public void LoadCardData(SOcard spawncard)
     {
         
         socard = spawncard;
 
         TitleData = socard.Title;                                         
         CardImageNormalData.sprite = socard.CardImage;
         PowerData = socard.Power;
         ResistanceData = socard.Resistance;
         CardImageLargeData = socard.CardImageLarge;
 
     }

}
next is script attached to deck

 public class DeckLogic : MonoBehaviour
 {
     public List<SOcard> card = new List<SOcard>();
     public SOcard socard;  //scriptableobject script
     public CardData CardTransfer;
 
     void Awake()
     {
         CardTransfer = FindObjectOfType<CardData>();
         socard = card.ElementAt(0);
 
     }
     public void addthiscard()
     {
         var SpawnCard = socard;
         CardTransfer.LoadCardData(SpawnCard);
 
     }

and script to my start button.

 public class Instantiatetest : MonoBehaviour, IPointerClickHandler
 {
     private DeckLogic decklogic;
     
    void Start()
     {
         decklogic = FindObjectOfType<DeckLogic>();
     }
     public void OnPointerClick(PointerEventData eventData)
     {
         
         GameObject card = Instantiate(Resources.Load("Card", 
         typeof(GameObject))) as GameObject;
 
         loadcard();
     }
     void loadcard()
     {
         decklogic.addthiscard();
     }

my idea is to click and instantiate card prefab at then transfer the first scriptableobject info from deck to that instantiated card prefab. when i clicked start button, i got this error;

gameobject reference is not set to an instance of an object.

if i created instance of the card prefab before transferin the info, it works. so i came to conclusion that both this code

GameObject card = Instantiate(Resources.Load("Card", typeof(GameObject))) as GameObject; and loadcard();

are executed at same time.

so now i am trying to find solution to execute the code in sequence.

or maybe i have wrong idea on how this works. im asking for any suggestion.

thanks in advance.

unity-201830f2-personal-battlefieldunity-xenos-tcg.png (149.4 kB)
Comment
Add comment · Show 2
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 Klarzahs · Jan 08, 2019 at 10:20 AM 0
Share

Welcome to the forum and unity :)

Please use the code format if you ask questions with code snippets. This increases the readability by a lot. You can convert already written text into code by clicking edit -> highlighting the code segments -> click on code sample (the "101010" button ontop of the edit window). Thanks

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by renomabum · Feb 14, 2019 at 04:37 PM

thanks. this is solved. with this simple code. pls forgive me for my noob question. still new to unity.

 SCRIPT 1
 // my scriptable object script eg.
 public string Name;
 public int Power;
 public int Defense;
 
 
 
 SCRIPT2
 //this script below is attached to start button
 // i am using event system here for click
 
 public GameObject AnyPrefab; //attach any prefab here from inspector
 public ScriptableObject CardData;//this is just example here. this can be accessed somewhere else maybe
 
 public void OnpointerClick(PointerEventData eventData)
 {
        var NewCard  = instantiate(AnyPrefab);
        NewCard.GetComponent<CardScript>().LoadCardData(CardData);
 }
 
 
 SCRIPT3
 //this script name below is CardScript which is attached to the prefab that i reference to AnyPrefab.
 
 public string Name;
 public int Power;
 Public int Defense;
 
 public void LoadCardData (ScriptableObject CardData )
 {
         Name = CardData.Name;
         Power = CardData.Power;
         Defense = CardData.Defense;
 }


cheers

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

98 People are following this question.

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

Related Questions

How do I access variables from a game object from an instantiated prefab 1 Answer

Dynamic Scene Creation through Scripts? 1 Answer

How to obtain this prefab name in this circunstances 1 Answer

Bool wont change on prefab clones 1 Answer

[SOLVED] Existed prefab countdown timer bar resetting it's value back to begining after i instantiate a new one. 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