• 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
Question by centaurianmudpig · Sep 26, 2011 at 08:05 PM · transformpositionparentbulletoffset

Setting parent in instantiated class

I am trying to get bullets to fire from a parented position. In the Weapon.cs Awake() function I create 2 gunports, pass the parent transform and a Vector3 to offset the position from the parent. When I run the code I get the following error from GunPort.cs:

NullReferenceException GunPort..ctor (UnityEngine.Transform _owner, Vector3 _offsetPosition) (at Assets/GunPort.cs:37) Starfighter.Awake () (at Assets/Starfighter.cs:63)

This error points to where I set the transform.parent to the passed _owner.transform. I have done something similar with parenting a camera to physical object with success and I dont know why there is a problem with the way I have done it here. The _owner is not null as it suggests which I verified from using the statement Debug.Log(_owner.ToString());. Please excuse the Itallic text it is not intentional. The only difference is that my GunPort does not have a physical object, its invisible, as intended.

Weapon.cs:

 void Awake () {
     gunPort = GetComponent<GunPort>();
     gunPorts = new GunPort[2];
     gunPorts[0] = new GunPort(transform, new Vector3(-10, 0, 0));
     gunPorts[1] = new GunPort(transform, new Vector3(10, 0, 0));
 }

 void CheckFiringPrimaryWeapon()
 {    
     if(shipControls.IsFiringPrimary)
     {
         Bullet clone;
         clone = Instantiate(bullet, gunPorts[0].Position, transform.rotation) as Bullet;
         clone = Instantiate(bullet, gunPorts[1].Position, transform.rotation) as Bullet;
     }
 }

GunPort.cs:

 public class GunPort : MonoBehaviour {
 private Transform owner;

 public Vector3 Position
 {
     get { return transform.position; }
 }
 
 public GunPort(Transform _owner, Vector3 _offsetPosition)
 {
     transform.parent = _owner.transform;
     transform.localPosition = 
         (Vector3.right * _offsetPosition.x) +
         (Vector3.up * _offsetPosition.y) +
         (Vector3.forward * _offsetPosition.z);
 }
 }
Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by syclamoth · Sep 27, 2011 at 06:26 AM

You shouldn't be using the Constructor on objects which derive from MonoBehaviour! The problem is that the object gets 'created' in a different way from how you would expect it to. Use Instantiate, and then pass in variables with an Init(Transform owner, Vector3 offset) method. Also, you don't need to use _owner.transform in your parenting line. _owner.transform will always return _owner, since it is already a transform! You may as well use

 transform.parent = _owner;

It is more efficient, since it does not use component lookups like Component.transform does.

Comment
Bunny83
centaurianmudpig

People who like this

2 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 centaurianmudpig · Sep 27, 2011 at 11:18 AM 0
Share

Ah, I see, thanks. Is there any way to instantiate the object without it needing to be attached to a GameObject, RigidBody, etc? As the GunPort will be invisible.

avatar image syclamoth · Sep 27, 2011 at 11:23 AM 1
Share

There is no need to attach a renderer to a gameObject, if you need it to be invisible! Just go GameObject -> Create Empty, and put your behaviours on that. If you don't need any of that GameObject stuff at all, then there's no need to inherit from MonoBehaviour at all!

avatar image

Answer by centaurianmudpig · Sep 27, 2011 at 02:59 PM

I think I get what you are saying though I am having some trouble trying to implement it. I'm quite new at unity so I hope you will bear with me. I created an empty GameObject, renamed it to GunPort, and attached my GunPort script to it.

In my Main.cs file I added:

 public GameObject port;

Then attached the GunPort GameObject to it within the Unity Editor Inspector.

In the Awake() function I Instantiated the GunPorts as follows (where Init replaces the constructor from the OP in GunPort.cs):

        gunPorts = new GunPort[2];
     gunPorts[0] = Instantiate(port) as GunPort;
     gunPorts[0].Init(transform, 1, new Vector3(10, 0, 0));
     gunPorts[1] = Instantiate(port) as GunPort;
     gunPorts[1].Init(transform, 1, new Vector3(-10, 0, 0));

The problem I have is that the Instantiate() does not return anything, as I get the error:

NullReferenceException: Object reference not set to an instance of an object Starfighter.Awake () (at Assets/Starfighter.cs:66)

The error happens on the Init() call, debugging shows that gunPorts[0] is null. I'm not sure what I have done wrong.

Comment

People who like this

0 Show 0 · 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

Answer by centaurianmudpig · Sep 27, 2011 at 02:58 PM

OK, I figured out where I went wrong. I should have created port as GunPort and not a GameObject. Thanks for your help in this.

Comment

People who like this

0 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 Bunny83 · Sep 27, 2011 at 03:58 PM 1
Share

Just fyi: Instantiate clones whatever object you provide as source as long it's derived from UnityEngine.Object. Instantiate will always return the same type that you provide. When you clone a Component like a script it automatically clones the owning GameObject and all attached components / subobjects. When cloning a GameObject it does the same but it will return a reference to the new GameObject.

However, if you want to create an empty GameObject with your script attached you can simply do:

 GunPort newGunPort = (new GameObject()).AddComponent<GunPort>();

The GameObject constructor also allows to automatically add components by their type-object:

 new GameObject("GO-name",typeof(GunPort));

But if you want a reference to the attached script you would have to call GetComponent so it's even more complicated:

 GunPort newGunPort = (new GameObject("GO-name",typeof(GunPort))).GetComponent<GunPort>();

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Controlling parent by Inputs 1 Answer

Following another object's position/rotation like parent/child relationship? 4 Answers

Transform position in editor confusion (screenshot examples) 1 Answer

Moving parent to position of child 0 Answers

Help! I want to make a following camera but in with Y position is constant ? 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