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 iamthecoolguy11 · Sep 16, 2014 at 02:40 AM · c#instantiatechild

How do instantiate stuff as a child

I made this script and looked all over the internet and tried everything I saw but I always get a error even tho I know I set it up properly but yet I still get a error. Atm I am using this set up and getting this error. If you could answer in code and not a link I well like that a lot better.

 read this before you assume its just fixing a error. 

 error
 Assets/Scripts/SpawnShips.cs(33,44): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)
 
 here's the script 


 using UnityEngine;
 using System.Collections;
 
 public class SpawnShips : MonoBehaviour {
     public string Help = "How many ships, type of ship, time befor start (keep it .5s or 1s";
     public Transform[] spawns;
     public GameObject[] SpaceShips;
     public Vector3[] positionArray = new [] { new Vector3(0f,0f,0f), new Vector3(1f,5f,5f) };
     private int currentPos = 0;
     private float time = 0;
     
     
     void Start () 
     {
         InvokeRepeating("spawn", 0, 0.5f);
     }
     
     void spawn()
     {
         int ships = Mathf.RoundToInt(positionArray[currentPos].x);
         int TypeOfShip = Mathf.RoundToInt(positionArray[currentPos].y);
         int TimeBeforStat = Mathf.RoundToInt(positionArray[currentPos].z);
 
         
         time += 0.5f;
         
         if(time == TimeBeforStat)
         {
             for(int i = 0; i < ships; i++)
             {
                 GameObject h;
                 h = SpaceShips[TypeOfShip];
                 GameObject instantiated = Instantiate(h.transform);            //instantiate the object
                 instantiated.transform.parent = spawns[i].transform;//assuming this script is attatched to the desired parent
                 instantiated.transform.localPosition = Vector3.zero;
             }
         }    
     }
     
 }
Comment
Add comment · Show 1
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 Kiwasi · Sep 16, 2014 at 03:20 AM 0
Share

Please provide the error text and line number.

If Unity throws an error you did not set it up properly.

4 Replies

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

Answer by kurotatsu · Sep 16, 2014 at 03:14 AM

Try this:

 for(int i = 0; i < ships; i++)
             {
                 GameObject h = (Instantiate(SpaceShips[TypeOfShip],spawn[i].position,spawn[i].rotation) as GameObject);
                 h.transform.parent = spawn[i].transform;
             }
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 iamthecoolguy11 · Sep 16, 2014 at 03:27 AM 0
Share

thanks man

avatar image kurotatsu · Sep 16, 2014 at 03:29 AM 0
Share

No prob, been there(just grabbed a snippet from an inventory I'm writing, and modified it for ya.).

avatar image
1

Answer by Cherno · Sep 16, 2014 at 02:55 AM

Change line 41 (wher you instantiate) to

  Transform instantiated = Instantiate(h.transform) as Transform ;  //instantiate the object

or

  GameObject instantiated = Instantiate(h.transform) as GameObject;  //instantiate the object
Comment
Add comment · Show 6 · 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 iamthecoolguy11 · Sep 16, 2014 at 02:59 AM 0
Share

That got rid of the error but can you also fix it cus it is not making it a child of the "spawns[i]"

avatar image Cherno · Sep 16, 2014 at 03:03 AM 1
Share

The line

instantiated.transform.parent = spawns[i].transform;

can be simplified. spawns already is of the type transform[], so you can just write

instantiated.transform.parent = spawns[i];

Now, as long as there is a transform in spawns at position i, the transform that is stored in instantiated will be made a child of this (spawns[i]) transform.

Also, if instatiated is of the type transform too, then the line can be made even shorter:

instantiated.parent = spawns[i];

avatar image iamthecoolguy11 · Sep 16, 2014 at 03:07 AM 0
Share

ok I tryed that but its still spawning as a clone and getting this error alt text

yet alt text

avatar image Cherno · Sep 16, 2014 at 03:17 AM 0
Share

Take a look at your for... loop.

  int ships = $$anonymous$$athf.RoundToInt(positionArray[currentPos].x);

Why is ships an int that gets defined by a rounded float? You use this variable to loop through an array so it may never have a value of more than the array's Length! If "ships" is 12 and spawns.Length is 11, you will keep getting the NullReference Error because you are trying to access a part of the array that is neither defined nor exists, but is completely outside it's boundaries!

avatar image iamthecoolguy11 · Sep 16, 2014 at 03:25 AM 0
Share

thanks for the help, but it is being rounded so that it can be a int

Show more comments
avatar image
1

Answer by Kiwasi · Sep 16, 2014 at 03:19 AM

The code to instantiate a child is pretty straight forward

 GameObject clone = (GameObject)Instantiate(prefab);
 clone.transform.parent = transform;

I suspect you have other things going wrong in your convoluted code. What is the actual error you are getting? Include the exact text of the error message and the line number.

I would also strongly recommend rewriting this script from scratch. Its a bit of a mess to follow.

Comment
Add comment · Show 3 · 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 iamthecoolguy11 · Sep 16, 2014 at 03:25 AM 0
Share

thank you that worked

avatar image iamthecoolguy11 · Sep 16, 2014 at 03:27 AM 0
Share

just one thing I dont see how its messy

avatar image Kiwasi · Sep 16, 2014 at 04:30 AM 0
Share

Using the x, y, z components of a Vector3 to store the number of ships, type of ship and start time is messy. Storing data as float and then rounding it to int is messy.

Suppose you decide you want to store a fourth piece of information with each wave? Your entire code structure is immediately broken. Your code will also break if the z component of the Vector3 is not a multiple of 0.5f.

It took me a dozen tries just to figure out what you were trying to do. Readable script is very important if you want to come back to it later, or if you want others to help you debug it. Trust me, you will always want to come back and make changes later.

Some ideas to tidy up the code:

Use a custom struct to record the wave data. That way you can modify wave data easily

 public struct Wave {
 
     public int noOfShips;
 
     // You could actually store the prefab here ins$$anonymous$$d of an int
     public int typeOfShip;
     public float startTime;
 }

 // With that struct in place you can call use
 int ships = wave.noOfShips;

 // Ins$$anonymous$$d of
 int ships = $$anonymous$$athf.RoundToInt(positionArray[currentPos].x);

Use input validation with custom properties to check you input for time is valid. Or use a coroutine ins$$anonymous$$d of invoke to call your waves at proper frequencies.

 IEnumerator spawn(){
     while (true){
         yield return new WaitForSeconds (wave.startTime)
         // Instantiate the wave
     }
 }

None of this code is perfect, but it should give you a glimpse of what can be done.

avatar image
0

Answer by MileSplit · Sep 16, 2014 at 02:47 AM

Use yourObject.transform.parent = parent

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 iamthecoolguy11 · Sep 16, 2014 at 02:51 AM 0
Share

could you put that in the script cus either im putting it in wrong or somthing els

avatar image iamthecoolguy11 · Sep 16, 2014 at 02:55 AM 0
Share

if you look at the script I have the same thing as your saying set up

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

27 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

Related Questions

Instantiated GameObject gets spawned as a child 2 Answers

Instantiating as a child error 1 Answer

Multiple Cars not working 1 Answer

How do you Instantiate Text as Child of Canvas c# 1 Answer

Making Instantiated Weapon a Child of the Player 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