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 FlannelFantasyXIV · Jan 23, 2013 at 11:25 PM · transformlightinglightspherecreation

Creating an object at another object's location

This is probably a dumb question, but I am just getting into Unity and need all the help I can get! :)

I have a lightswitch that I want to make a sphere appear at when it is triggered. The code I have thus far is:

 var lightObject = GameObject.Find("LightCube");
 
 function OnCollisionEnter(collision : Collision) {
     light.enabled = !light.enabled;
   
   if(light.enabled){
      var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
      sphere.transform.position = lightObject.transform.position;
     }
     
 }

However, when the sphere is created, it is made at coordinates 0,0,0, rather than the coordinates of the light. What exactly am I doing wrong?

Thanks in advance!

Comment
Add comment · Show 3
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 AlucardJay · Jan 23, 2013 at 11:25 PM 0
Share
 var lightObject = GameObject.Find("LightCube");

This needs to be within a function to work =]

 var lightObject : GameObject

 function Start()
 {
     lightObject = GameObject.Find("LightCube");
 }
 
 function OnCollisionEnter(collision : Collision) {
     light.enabled = !light.enabled;
     
     if(light.enabled){
         var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         sphere.transform.position = lightObject.transform.position;
     }
 }
avatar image FlannelFantasyXIV · Jan 23, 2013 at 11:30 PM 0
Share

Thank you so much! :)

avatar image AlucardJay · Jan 23, 2013 at 11:37 PM 0
Share

For future reference, you may want to check that a lightObject has been found first, to avoid the dreaded Null Reference Exception :

 var lightObject : GameObject

 function Start()
 {
     lightObject = GameObject.Find("LightCube");
 }
 
 function OnCollisionEnter(collision : Collision) {
     light.enabled = !light.enabled;
     
     if(light.enabled){
         var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         if ( lightObject ) { // means lightObject is not equal to null
             sphere.transform.position = lightObject.transform.position;
         }
     }
 }

1 Reply

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

Answer by Matthew0123 · Jan 23, 2013 at 11:32 PM

You can not declare a variable outside of a function so this needs to be done.

 var lightObject = GameObject.Find("LightCube"); // Change this to var lightObject: GameObject;
 
 function OnCollisionEnter(collision : Collision) {
     // Put this line in here. lightObject = gameObject.Find("LightCube");
 /*
 GameObject and gameObject are two different things. A GameObject is (I forgot the word for it) a specifier of the variable. Like for example, var pi: float; It it saying this can only be filled by a number and this number can have decimal points.
 
 gameObject is talking about the object that can be found in the scene at that point so gameObject is referring to the scene and what is in it. So please be careful at this. It took me like a day or two to understand how it works. I maybe started using unity a month ago and I am starting to be able to answer basic questions such as these.
 */
     light.enabled = !light.enabled;
 
   if(light.enabled){
      var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
      sphere.transform.position = lightObject.transform.position;
     }
 
 }
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

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

11 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

Related Questions

Gradually change light intensity 0 Answers

How to make a GameObject not hittable by shadows ? 2 Answers

Sphere and text mesh in prefab share transformation matrix 0 Answers

Do I still need lightmaps? 1 Answer

How to prevent Overlapping Lights from combining / adding intensity? 3 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