Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 hedgehogcn · Nov 22, 2017 at 11:48 PM · gameobjectinstantiatevariablecs0119cs0118

Simple Dice Game - Error with Variable within an Instantiated Game Object (Prefab) - CS0118 and CS0119 Errors

Hello Unity Community! Trying to make a simple dice rolling game with two players.


I have an empty game object with the PlayerSetup class attached. The purpose of this script is to create a prefab for each player (2). The prefab has a class attached (as a component) called PlayerDice - within this script it calls the DiceRoll class for each dice that is rolled.


What I am trying to do is pass a value through to the PlayerDice class and change the value of the numDice for each prefab. However, it throws me two errors:


Assets/Scripts/PlayerSetup.cs(31,32): error CS0118: 'PlayerDice' is a 'type' but a 'variable' was expected

Assets/Scripts/PlayerSetup.cs(31,32): error CS0119: Expression denotes a 'type', where a 'variable', 'value' or 'method group' was expected


So clearly something is wrong, I just don't know what. Any thoughts/help? (Below is my code snippets


 public class PlayerSetup : MonoBehaviour {
 
     Dictionary<string, int> _PlayerSetup = new Dictionary<string, int>();
 
     public Rigidbody playerPrefab;
 
     void Start () {
         //Player Setup
         _PlayerSetup.Add("Chad", 4);
         _PlayerSetup.Add("Bill", 4);

         //Position is not necessary, just doing it for fun
         float x = 5;
         float z = 5;
 
         //Cycle through the dictionary of players, create them off a prefab
         foreach (KeyValuePair<string, int> item in _PlayerSetup)
         {
 
             Rigidbody playerInstance;
 
             playerInstance = Instantiate(playerPrefab);
             playerInstance.name = item.Key;
             playerInstance.GetComponentInChildren<TextMesh>().text = item.Key;
             playerInstance.transform.position = new Vector3(x, .5f, z);
             playerInstance.GetComponent(PlayerDice).numDice = item.Value;
             
             x = Random.Range(-5, 5);
             z = Random.Range(-5, 5);
         }
     }
 }



 public class PlayerDice : MonoBehaviour {
 
     public int numDice = 1;
     Dictionary<string, List<int>> _PlayerResults = new Dictionary<string, List<int>>();
 
     void Start()
     {
         //Create a new dice roll
         var dice = gameObject.AddComponent<DiceRoll>();
         
         var diceResults = new List<int>();
         int num = 0;
         int x = 0;
 
         //Rolls a 6 sided dice for each number of dice the player has, stores it into an array
         while (num < numDice)
         {
             x = dice.RollDice(6);
             diceResults.Add(x);
             Debug.Log("Dice roll: " + diceResults[num]);
             num++;
         }
         Debug.Log("Highest Roll: " + diceResults.Max());
         
         //NOTimplemented yet - I need to pass the item.Key (player name) value through...
         //_PlayerResults.Add(item.Key, diceResults);
     }
 }



 public class DiceRoll : MonoBehaviour {
 
     public int RollDice(int diceSides)
     {
         int result = Random.Range(1, diceSides - 1);
         return result;
     }
 }


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

2 Replies

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

Answer by jeffreyrampineda · Nov 23, 2017 at 01:09 AM

In your PlayerSetup class, try this :

playerInstance.GetComponent<PlayerDice>().numDice = item.Value;

instead of this:

playerInstance.GetComponent(PlayerDice).numDice = item.Value;

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 hedgehogcn · Nov 23, 2017 at 05:40 PM

That worked, thanks much! Happy thanksgiving.

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.

Follow this Question

Answers Answers and Comments

117 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 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

Calling a variable 2 Answers

Passing a gameobject through two methods 0 Answers

ArgumentException: The Object you want to instantiate is null. 1 Answer

How to spawn a GameObject by name? 2 Answers

Only change a variable on the instaniated object not the prefab. 0 Answers


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