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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Aidz · Dec 04, 2014 at 05:43 AM · c#prefabarraysarray of gameobjects

Obtaining the Variable of a Prefab in an array.

Hi Guys,

Still trying to make this Blackjack game, I've gotten my cards to spawn upon a keypress, but what I want to do is grab the variable of the card prefabs which are being spawned and placed in an array.

Here is the code:

(Box Controller Script)

 public GameObject[] cardsInBox;
 private Vector3 cardPosition = new Vector3 (.1f, .01f, -.1f);
 public int boxTotal = 0;
 public int cardCount = 0;
 public GameObject card;


 void Update ()
 {
     if (Input.GetKeyDown(KeyCode.H))
         {
         BoxHit();
         Debug.Log (boxTotal);
         }
 }
     void BoxHit()
 {
             cardsInBox[cardCount] = Instantiate(card, transform.position + cardPosition * cardCount, transform.rotation) as GameObject;
             cardsInBox[cardCount].name = "PlayerCard" + cardCount.ToString();
             boxTotal += cardsInBox[cardCount].GetComponent<Cards>().cardValue;
             Debug.Log(cardsInBox[cardCount].GetComponent<Cards>().cardValue);
             cardCount++;
 }



Inside the GameObject card is where I attach the card prefab in the inspector. When card is instantiated, it runs this script to determine its value.

     public int cardValue = 1;

 void Start () 
 {
     cardValue = (int)Random.Range (1,13);
 }


At the moment, when I hit H, the card spawns fine, and each prefab within the cardsInBox array has it's own value which I can see in the inspector, however boxTotal increments only by 1 and not the Spawns prefab variable, I suspect I'm simply grabbing the card Prefabs value before it is intantiated, but I don't know how to access the variable of the prefab that has been spawned, any ideas?

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

1 Reply

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

Answer by MD_Reptile · Dec 04, 2014 at 06:11 AM

If your are just instantiating it, it may not run its Start() method until after you check for the value of "cardValue" therefor giving it the default value. What you could do, that MIGHT work (I am not totally sure this is what is happening I could always be wrong lol) is instead of having it set its value in Start() of the cards own script, instead assign that value just after creation, and before using the value.

So this part:

    cardsInBox[cardCount] = Instantiate(card, transform.position + cardPosition * cardCount, transform.rotation) as GameObject;
          cardsInBox[cardCount].name = "PlayerCard" + cardCount.ToString();
          boxTotal += cardsInBox[cardCount].GetComponent<Cards>().cardValue;
          Debug.Log(cardsInBox[cardCount].GetComponent<Cards>().cardValue);
          cardCount++;

Would become:

        cardsInBox[cardCount] = Instantiate(card, transform.position + cardPosition * cardCount, transform.rotation) as GameObject;
 cardsInBox[cardCount].GetComponent<Cards>().cardValue = (int)Random.Range (1,13);
              cardsInBox[cardCount].name = "PlayerCard" + cardCount.ToString();
              boxTotal += cardsInBox[cardCount].GetComponent<Cards>().cardValue;
              Debug.Log(cardsInBox[cardCount].GetComponent<Cards>().cardValue);
              cardCount++;

Good Luck!

Comment
Add comment · Show 2 · 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 Aidz · Dec 04, 2014 at 06:30 AM 1
Share

Thanks for the quick reply!

You have saved me a whole lot of stress, I was originally thinking that it may not have generated yet and I tried to use yield/invoke but that wasn't working for me.

This fully worked for me, thank you!

avatar image MD_Reptile · Dec 04, 2014 at 06:35 AM 0
Share

No problem, glad I could 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

26 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

Related Questions

Prefabs instantiated from an array are keeping their public int value 1 Answer

Multiple Cars not working 1 Answer

How to effect and apply a common material to children individually... 2 Answers

Adding a prefab to GameObject[] via script 1 Answer

Array of Arrays of GameObjects/Prefabs (C#) 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