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 /
This question was closed Sep 23, 2013 at 12:21 PM by Ranger-Ori for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ranger-Ori · Feb 17, 2013 at 04:58 PM · monobehavioursingletondesignpattern

How to interact game objects?

Hello everyone,

I'm familiar with the Unity environment, but lately I have many troubles with my code. I used to work with Unity Script, and I converted my work to C#, my question is, how do you interact with other MonoBehaviour game objects?

I tried using the Singleton pattern, and also used the Factory pattern, and these has it's limits, and I was wondering if anyone here could give me his advice or anywhere to read, how to access game objects, classes, and code, without the "Send Message" or "Broadcast Message" as I find these, un-effective methods.

Comment
Add comment · Show 1
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 tavoevoe · Feb 17, 2013 at 05:09 PM 0
Share

I usually use either GameObject's "Find" method. You can call it once on start and keep the reference forever. Otherwise, you can create an uninstatinated GameObject variable and assign a value to it in the inspector.

Is that what you're looking for?

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by tuhinbhatt · Feb 17, 2013 at 05:27 PM

Here's is how i usually access the other GameObjects:

First Method: Declare the GameObject as public for example

 public GameObject myobject;

Then drag the GameObject we want to access to myobject in the inspector. You can also directly access the particular component of the gameobject as that would be more effecient.

For Example:

 public Transform transform;

Then you can just drag the gameobject to transform in the inspector and you can access the Transform Component of the dragged gameobject.

Second Method:

You can use

 GameObject.Find("Gameobjectnamehere");

or

 Gameobject.FindGameObjectWithTag("");  


This is ofcourse less effecient than the first method described above.

For getting a Component of any particular gameobject you can use the following

Simple Example Here:

 public GameObject otherobject;
 Transform otherobjecttransform=otherobject.transform;

You can also use this:

  //Ignore the space below in angular brackets as thats how this site is designed..
  Transform otherobjecttransform=GameObject.Find("otherobject").GetComponent
  <    Transform   >();


This above code line will Find the GameObject name otherobject and get it's TransForm Component

Public Static Variables

You can Create A public Static Variables in the Script so that they can accessed in other Script

Here is the simple example:

 public static int score;


Just for understanding assume that this variable is in Script called global.cs. Now you can easily access this variable anywhere(in any script) using

   global.score; 
   int getscore=global.score;

Not only that you can also create public static methods in C# script.

For Example:

 public static void printmyname()
 {
 Debug.Log("MyName");
 }

Now assuming that this method is in script called global.cs you can access it anywhere just by calling this method as belows:

 global.printmyname();

Do note that a static method must contain static variables.

Check this page Accessing Other GameObjects in Refrence Doc for detailed explanation as well as sample code. Ofcourse select C# instead of JavaScript for code and also do check this Accessing other Components .

Please if anybody who knows more please comment on this if i have made some mistake as well as make suggestions and suggest new ways

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 Ranger-Ori · Feb 17, 2013 at 10:15 PM 0
Share

Thank you for your time, but I don't think you understood me correctly. I asked a question more of how accessing game objects and their classes in a more of design pattern way, and not by Unity tools.

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

Singleton without MonoBehaviour? 1 Answer

Creating a singleton game manager 1 Answer

Design Patterns (Singleton Issue) 1 Answer

How can I return a GameObject? 1 Answer

Using the Delegate Object design pattern in Unity 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