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
0
Question by Basso · Jun 04, 2013 at 08:11 AM · c#instantiateprefabnameid

ID or name for Instantiate prefab

Im trying to create a Big Die out of small ones, so far it works but my problem is to give each small dice a id or name, i searched for a while and it doesn't work, this is a part of my code:

         using UnityEngine;
         using System;
         using System.Collections;
         using System.Collections.Generic;
         
         public class CreateDiceV1 : MonoBehaviour {
             
             public Transform prefab;
             public int Anzahl = 4;
             
             void Start () {        
                 
                 float Abstand = 1.5F;
                 
                 int IDCounter = 0;
                 int AllDiceCounter = ( ( ( (int)Math.Pow(Anzahl,2) ) * 2 ) + ( ( ( Anzahl - 2 ) * Anzahl ) * 2 ) + ( ( (int)Math.Pow((Anzahl-2),2) ) * 2 ) );    
                 Debug.Log("This Many Dice -> " + AllDiceCounter);   
                 
                 //Front
                 for (float x = 0; x < Anzahl; x++) {
                     for (float y = 0; y < Anzahl; y++) {
                         GameObject GameObj = Instantiate(prefab, new Vector3(  x * - Abstand + Abstand * ( Anzahl / 2 ) - 0.75F, y * - Abstand + Abstand * ( Anzahl / 2 ) - 0.75F, 0 * - Abstand + Abstand * ( Anzahl / 2 ) - 0.75F), Quaternion.identity) as GameObject;
                         GameObj.name = "TEST"+IDCounter;
                         IDCounter++;
                     }            
                 }    
             }    
         }
 

I saw the solution with the gameobject in several places but i just doesnt work, i got this error message:

NullReferenceException: Object reference not set to an instance of an object CreateDiceV1.Start () (at Assets/CreateDiceV1.cs:28)

I also tried to create it as a public var, doesn't work either.....

Can you help me or at least have a hint for me?

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 Fattie · Jun 04, 2013 at 08:13 AM 0
Share

please note that the word

DICE IS A PLURAL

The singular is "die". One die, two dice, three dice. This is quite surprising to many non-English speakers. The die is cast.

.name is correct, you have some other simple problem. You should indicate clearly (paste it) which line is throwing the error.

Also .. You cannot have very long lines of code like that. Carefully rewrite it over a few lines.

avatar image Basso · Jun 04, 2013 at 08:23 AM 0
Share

The error is because of this line:

 GameObj.name = "TEST"+IDCounter;

I understand the meaning of the error but i cant figure why its happening, because i just fill the object the line before.....

1 Reply

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

Answer by syclamoth · Jun 04, 2013 at 08:40 AM

The problem is that you are trying to instantiate a 'Transform' as a 'GameObject'. Change the type of the prefab variable to 'GameObject' and the problem will go away (you will, of course, have to re-assign the prefab in the inspector.

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 Basso · Jun 04, 2013 at 08:44 AM 0
Share

thanks, that solved my problem.

avatar image Ashkan_gc · Jun 04, 2013 at 12:55 PM 0
Share

Yep he is right. Technically speaking when you instantiate a gameObject with a reference to a transform of it, you can not cast it to gameObject so there reference should be in the type that you want to cast the result of instantiate to.

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

17 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

Related Questions

instantiating vertically 2 Answers

Instantiation in C# 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

How do I make prefab follow a gameobject?(C#) 1 Answer

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 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