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 /
avatar image
1
Question by wolis · Jun 15, 2013 at 01:38 PM · gameobjectinstantiateobjectclassclasses

Instantiating gameObject with custom Class properties

Hi guys! I`m just starting to learn about classes and although I can grasp the basic concepts of what a class is, some things are making my head ache. So basically I have made class that holds some properties. I now want to spawn a cube, that is of this class, so that I would have a cube, that already has the properties defined by me.

Heres that last version of code I tried:

 public static int arrLen=8;
 public static int arrWid=8;
 public GameObject cube;
     
 public Cell[,] cellsArr = new Cell[arrLen,arrWid];
 private Cell cell;    
 public class Cell
 {
     public float x;
     public float y;
     public int state;
     public GameObject prefab;
     public GameObject cellRef;
     public Transform parentTransform;
         public Cell(float x, float y,GameObject cellFab)
         {
             this.x =x;
             this.y = y;
             this.prefab = cellFab;
         }       
 }
 
 void Awake()
 {
     //instantiate the whole array of cubes
     for (int i = 0; i<arrLen; i++)
     {
         for(int j = 0; j<arrWid; j++)
         {
             cell = Instantiate (cube, new Vector3(i,j,0),Quaternion.identity) as Cell;
             
             cellsArr[i,j] = cell;
 
         }
     }
 }

Now I get this error: Assets/Scripts/Algorithm.cs(47,91): error CS0039: Cannot convert type UnityEngine.Object' to Algorithm.Cell' via a built-in conversion

What am I doing wrong and what would be a possible solution?

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
2
Best Answer

Answer by Jamora · Jun 15, 2013 at 03:22 PM

You cannot instantiate your own classes (unless they're ScriptableObjects). Instead, you either instantiate a GameObject and add your own class as component, or instantiate a prefab, that already has yor custom class added to it.

I assume your cube is a prefab that has your script added, so try the following:

 GameObject temp = Instantiate (cube, new Vector3(i,j,0),Quaternion.identity) as GameObject;
 cellsArr[i,j] = temp.GetComponent<this>().cell;

For my snippet to work, you'll need to make your cell variable public and have your cell initialized.

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 wolis · Jun 16, 2013 at 07:18 AM 0
Share

Thanks mate! :) I understand that this code gets the class properties from the script attached to the object. How would I then make an object have a class properties that are defined in script I am instantiating the object from?

avatar image Jamora · Jun 16, 2013 at 03:09 PM 2
Share

You would replace temp.GetComponent<this>() by whatever type you want to get from the GameObject (that also has a public Cell cell... If I understood you correctly.

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

16 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

Related Questions

how to instantiate object when I click 1 Answer

object not spawning but no error message 2 Answers

Calling an object from a different JS Script and Instantiate it 1 Answer

Alternative to using prefabs w/scripts to save sets of data for use at runtime? 1 Answer

C# cast object to gameobject not working 2 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