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 thedodgeruk · Oct 29, 2012 at 03:14 PM · gameobjectinstantiatetransform

create a plain GameObject through script

this is what i have at the mo public class FakeCollider : MonoBehaviour {

     public GameObject  m_realcolliderObject;
     private GameObject m_triggerObject;
 
     void Start()
     {
         Instantiate(m_triggerObject, m_realcolliderObject.transform.position, m_realcolliderObject.transform.rotation);
 
         m_triggerObject.gameObject.AddComponent<BoxCollider>();
         m_triggerObject.collider.isTrigger = true;
         m_triggerObject.GetComponent<BoxCollider>().size   = m_realcolliderObject.GetComponent<BoxCollider>().size;
         m_triggerObject.GetComponent<BoxCollider>().center = m_realcolliderObject.GetComponent<BoxCollider>().center;
 
     }
 
     Vector3 m_position =  new Vector3(0,-100,0);
     // Update is called once per frame
     void Update () 
     {
         m_triggerObject.transform.rotation = m_realcolliderObject.transform.rotation;
         //m_position
         m_position.x = transform.position.x;
         m_position.z = transform.position.z;
         m_position.y = 20;
         m_triggerObject.transform.position = m_position;
         
     }
 }

the idea is that i create a plain gameobject athe the same place as the other game object , create a collider , assign the same size and postion of the collider , make it a trigger and offset it each update to position.y = -100;

it gives a null refernce for the Instanceate.

i have tryed

 m_realcolliderObject.AddComponent<GameObject>();

but it does not like this eaither

any ideas please?

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

Answer by hoffmanuel · Oct 30, 2012 at 07:06 AM

This creates an empty GameObject:

 GameObject yourName = new GameObject();
Comment
Add comment · 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
0

Answer by hoffmanuel · Oct 29, 2012 at 03:24 PM

take a look to the documentation:

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

the instantiate function returns a gameobject. the first parameter to the instantiate function considers a prefab, not the gameobject, that hold the newly instantiated gameobject in your class.

more correct would be:

 m_triggerObject = Instantiate("YourPrefab", m_realcolliderObject.transform.position, m_realcolliderObject.transform.rotation);
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 thedodgeruk · Oct 29, 2012 at 08:14 PM 0
Share

i just want to add a plain GameObject though

avatar image hoffmanuel · Oct 30, 2012 at 07:03 AM 0
Share

You mean an empty one? than your code will be for the cration in start:

 m_triggerObject = new GameObject();
 m_triggerObject.transform.position = m_realcolliderObject.transform.position;
 m_triggerObject.transform.rotation = m_realcolliderObject.transform.rotation;
 
 m_triggerObject.AddComponent();
 m_triggerObject.collider.isTrigger = true;
 m_triggerObject.collider.size = m_realcolliderObject.collider.size;
 m_triggerObject.collider.center = m_realcolliderObject.collider.center;

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

10 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

Related Questions

Instantiate Terrain Object as child of Empty Game Object 1 Answer

Instantiate from array into array? 2 Answers

Creating new Transform from existing objects Transform to Instantiate object 1 Answer

Why does this Instantiate give a Null Reference Exception? 1 Answer

Adjusting Rotation of Instantiated Objects 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