Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 khatake · Dec 08, 2011 at 10:44 AM · c#instantiateobjectparentparenting

Manipulating Instanced Object

Hi, my goal is to Instantiate an object at a start of a game and Set its parent to a pawn. The function that creates Instances looks like so :

     void CreateRocket_L()
 {
         Rocket_L = Instantiate(rockets, 
                     new Vector3(TopDown_Controller.Instance.transform.position.x + 0.85f,
                                 TopDown_Controller.Instance.transform.position.y,
                                 TopDown_Controller.Instance.transform.position.z), 
                     Quaternion.identity) as rocket;        

     Debug.Log(Rocket_L);
         //Rocket_L.transform.parent = TopDown_Controller.Instance.transform;
 }

The problem is that Debug.Log shows that Rocket_L is Null... If I try to do anything to Rocket_L Unity says there is no reference to an object in Rocket_L (Debug.Log agrees). Setting parent after Debug.Log is commented out since Unity says Rocket_L is null but I left it there so you could see whats the point. I found in Unity Script Reference:

     public class example : MonoBehaviour {
 public Rigidbody projectile;
 void Update() {
 if (Input.GetButtonDown("Fire1")) {
 Rigidbody clone;
 clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
 clone.velocity = transform.TransformDirection(Vector3.forward * 10);
 }
 }
 }

The principle is the same here but they set the velocity of a clone object with no problem. Any idea why my isn't working ?

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 aldonaletto · Dec 08, 2011 at 11:06 AM

I suspect your problem is a confusion about types. Instantiate returns the same type of the prefab, thus you should have a rocket type defined elsewhere, and the variables rockets and Rocket_L should be of type rocket - a bad idea, unless you know exactly what you're doing with this custom type rocket.
You can simplify things: declare rockets and Rocket_L as Transform, like below:

Transform rockets; // rocket prefab Transform Rocket_L; // instantiated rocket

void CreateRocket_L() { Vector3 pos = TopDown_Controller.Instance.transform.position; pos.x += 0.85f; // <- using a different way to specify position Rocket_L = Instantiate(rockets, pos, Quaternion.identity) as Transform;
Debug.Log(Rocket_L); Rocket_L.parent = TopDown_Controller.Instance.transform; } NOTE: I used the pos auxiliary variable just to simplify the writing - this has nothing to do with the problem you had.

Comment
Add comment · Show 1 · 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 aldonaletto · Dec 08, 2011 at 01:25 PM 0
Share

Instantiate(obj,...) clones the object obj; if obj is a component like Rigidbody, Transform, Renderer etc. the whole GameObject to which the component belongs is cloned, and the reference returned is the same component passed to Instantiate. In the docs example, a Rigidboy was passed to Instantiate: the whole GameObject was cloned, and the reference returned was its Rigidbody.
In your specific case, you passed a rocket reference, which isn't part of UnityEngine - I suspect that Unity may even have cloned the object, but having found no rocket component, returned null.
Passing its Transform, the GameObject will be cloned and its transform returned, but probably you will have to instantiate yourself the rocket class, and assign the new object to it.

avatar image
0

Answer by khatake · Dec 08, 2011 at 12:45 PM

Ohhh sorry I didn't mention but i do have class rocket. But I will try doing it with Transform.

Comment
Add comment · Show 1 · 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 aldonaletto · Dec 08, 2011 at 01:13 PM 0
Share

@khatake, please click the more button and convert this answer to a comment in my answer (the Your box must be used only to answer the question; comments and replies must be posted with add new comment).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Randomly instantiate objects from array without choosing the same item twice. 2 Answers

Trying to instantiate prefabs to a parent object with C# script. 1 Answer

attach parent to instantiated object c# 1 Answer

Help with Instantiate and parents 1 Answer

Instantiate Problem 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