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 JJa · Apr 29, 2011 at 04:48 AM · gameobjectinstantiatespawn

Spawning GameObject contained within another

Here is what I am trying to do. I have a gameobject. When a box collides with it, it should spawn another gameobject. I followed this link http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html . My code is pasted below:

var purpleTriangleBottomOuterCornerMelty : GameObject; function OnCollisionEnter(event : Collision) { Debug.Log("collision " + name); createMelty(name, transform.position); }

function createMelty(objectName: String, objectCoords: Vector3) { Debug.Log("instantiating " + meltyMappingList[objectName]["rotation"]); Instantiate(purpleTriangleBottomOuterCornerMelty, objectCoords, Quaternion.Euler(0, meltyMappingList[objectName].rotation, 0)); }

I have set the purpleTriangleBottomOuterCornerMelty variable to the prefab that I want to spawn, in the inspector of the object containing this script. But the Instantiate gives me a NullReferenceException. Apologies if I've missed something obvious, but I'm a total noob.

PS The above script is in my spawning object. I have a number of spawing objects, and want to spawn a different GameObject depending on the spawning object, so I made a mapping object (not included in the code) to map spawning object name to spawnee object.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by loopyllama · Apr 29, 2011 at 07:10 AM

To me, it looks like this:

meltyMappingList[objectName]["rotation"])

and this:

meltyMappingList[objectName].rotation 

are what I would concentrate on. Something isn't quite right with those two. You didn't include what the meltyMappingList is, but how can meltyMappingList[string][string] and meltyMappingList[string].rotation both be correct? Are you sure you didn't want something like:

meltyMappingList[int]+ ".rotation")

and:

meltyMappingList[int].rotation
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 JJa · Apr 29, 2011 at 03:35 PM 0
Share

Yeah I agree, I'm not entirely sure what's going wrong there either. objectName and "rotation" are both string keys that give values. It has a general structure like: var melty$$anonymous$$appingList = { //Top half "ltPrplTriangle1" : {"melty": "purpleTriangleTop$$anonymous$$iddle$$anonymous$$elty", "rotation" : -45}};

where "ltPrplTriangle1" is one of the expected values of objectName

avatar image JJa · Apr 29, 2011 at 03:36 PM 0
Share

But the basic problem is that it's saying that purpleTriangleBottomOuterCorner$$anonymous$$elty is null, even though I've clearly assigned it in the Inspector

avatar image loopyllama · Apr 29, 2011 at 07:57 PM 0
Share

with this info I can only guess. can you instaniate this prefab in another script? can you instantiate a cube in this script! can you show the entire script and paste in the error message? It is probably something simple, but not obvious from the info in the question...

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

No one has followed this question yet.

Related Questions

How should i add raycast to multiple game objects??? 0 Answers

Spawn items around GameObject 3 Answers

object not spawning but no error message 2 Answers

Problem with spawn object 0 Answers

How to spawn gameobject from host to all clients 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