Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by alexapavlovich · May 19, 2016 at 08:42 PM · unity 5objectslightsdefine

Define and use any object from a certain class

My map has multiple objects and lights, but I do not know how to use these objects/lights in my C# class. How can I define these objects/lights and use a certain object/light by using its name. Like for example I have a Cube with a name "C1" and a Spotlight "S1" and how can I tell the class to use these certain objects.

Thanks in advance

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

Answer by Ahndrakhul · May 19, 2016 at 09:51 PM

I think this is what you are looking for. This is just an example of getting references to your scene objects using GameObject.Find, and then using those references to move things and change various properties.

 using UnityEngine;
 
 public class SampleClass : MonoBehaviour {
 
     GameObject myCube;
     GameObject mySpotlight;
     Light spotlightLight;
 
     void Start ()
     {
         myCube = GameObject.Find("C1");
         mySpotlight = GameObject.Find("S1");
         spotlightLight = mySpotlight.GetComponent<Light>();
         ChangeLightColor(spotlightLight, Color.red);
     }    
 
     void Update ()
     {      
         myCube.transform.Translate(Vector3.up / 100);
         mySpotlight.transform.Translate(Vector3.back / 100);
         spotlightLight.intensity += .01f;
         spotlightLight.range += .1f;
         spotlightLight.spotAngle += .05f;
     }
 
     void ChangeLightColor(Light light, Color color)
     {
         spotlightLight.color = color;
     }
 }

You can also do this by making the fields (myCube, mySpotlight, Light) public and then dragging your objects onto the slots on the script in the inspector.

 using UnityEngine;
 
 public class SampleClass : MonoBehaviour {
 
     public GameObject myCube;
     public GameObject mySpotlight;
     public Light spotlightLight;
 
     void Start ()
     {
         ChangeLightColor(spotlightLight, Color.red);
     }    
 
     void Update ()
     {      
         myCube.transform.Translate(Vector3.up / 100);
         mySpotlight.transform.Translate(Vector3.back / 100);
         spotlightLight.intensity += .01f;
         spotlightLight.range += .1f;
         spotlightLight.spotAngle += .05f;
     }
 
     void ChangeLightColor(Light light, Color color)
     {
         spotlightLight.color = color;
     }
 }

 

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 alexapavlovich · May 20, 2016 at 07:48 AM 0
Share

Thanks, this was really helpful.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Objects imported from Rhinoceros missing 1 Answer

Object movement algorithm 0 Answers

Can't move camera when scipt's attached to chosen gameobject 1 Answer

How to control an object after clicking on it? 1 Answer

How can i reach other object in script of a object? 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