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 Syley · Mar 18, 2016 at 12:50 PM · gameobjectcomponentvaluecommunication

Passing references between game objects

Hi guys,

I've had a look but haven't been able to find anything quite regarding this topic. Most of what I've found has involved communication between components on a single game object whereas I want information between different game objects. I think explaining what I want to do and how I'm going about it would probably result in a better answer from you guys in case my train of thought it incorrect so here it goes.

Basically I'm trying to make a tactics game where players will click on their units and then select an area to move to. I've been investigating ways to detect hits based on mouse clicks and saw you could do it with raycasts from a camera. Some sample code made this seem easier than detecting a click position every frame and then checking that position. Here is what I've got for my main camera so far:

 using UnityEngine;
 using System.Collections;
 
 public class ClickDetecter : MonoBehaviour {
     Camera camera;
     public GameManager gameManager; 
     // Use this for initialization
     void Start () {
 
         camera = GetComponent<Camera>();
         gameManager = GetComponent<GameManager>();
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0))
         { // if left button pressed...
             Ray ray = camera.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 gameManager.HandleHit(ray, hit);
 
             }
         }
     }
 }


Now the problem is that I've got my gameManager object in another component with a few other scripts to help make my game work. This is why GetComponent does not seem to be grabbing anything. Now I guess I could copy the script into the Camera component but then I'm not sure how it would react, would it copy the instance or create a new one for example?

I guess from a coding perspective what I want to do it just pass a reference of the currently initialised game manager I have into the camera and then just use that, is this possible or will they all have to be on the same component?

One idea I've had is to just copy the rest of the scripts into the camera since the other game object <'m using for my scripts isn't too important by itself but I'm just curious if there are any other better ways or if my train of thought completely is wrong.

The reasoning behind wanting my game manager to handle the hit is just because a lot of information is stored in there so I thought it would be easier to just pass in the hit information and deal with it through that avenue.

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

Answer by Zorkman · Mar 18, 2016 at 01:32 PM

Hello,

Please check trough your spelling when asking questions. You state

Now the problem is that I've got my gameManager object in another component with a few other scripts to help make my game work.

I guess you meant:

Now the problem is that I've got my gameManager component in another gameobject with a few other scripts to help make my game work.

If that's the case, you can use

 GameObject go = GameObject.Find("NameOfTheGameObjectThatHasTheGameManager");
 GameManager gameManager = go.GetComponent<GameManager>();
 gameManager.HandleHit(ray, hit);

or

 GameManager gameManager = FindObjectOfType<GameManager>();
 gameManager.HandleHit(ray, hit);

However GameObject.Find() and FindObjectOfType<Type>() is not so great in terms of performance. I recommend at some point you look into the singleton pattern which is great for managers.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a way to refer to the only script the object has without naming the script? 2 Answers

How to make something like a point effector 3D? 1 Answer

Get components on a gameObject after build. 0 Answers

How do I remove ALL components from a gameObject? 3 Answers

How to get a custom component declared by variable? 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