• 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 SweetPotatoes · Dec 07, 2014 at 11:01 PM · c#communication

Issue with sending information between two scripts

Hey everyone.

I have a script that detects when a player has fallen onto the floor which works fine, and the other script which I want to take that information to then end the game and switch to the results were the players can restart the match, go into the option, or exit the game/main menu.

The script that detects the player(s) is attached a GameObject while the other script is part of a simple state machine and isn't attached anything.

Because I am new to all of this I am having trouble figuring how to send that information from one script to the other.

This is the script that detects

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDetection : MonoBehaviour {
 
     public void OnCollisionEnter(Collision col)
     {
         //TEST!!! Only upper body box collider is in use for the current test. 
         //Must inclued both colliders on each leg and arms, along with the head and lower body.
 
         if(col.gameObject.name == "bn_spineIK_d001")
         {
 
             Debug.Log("Player has touched the floor, one point to other player. GAME OVER!");
         }
     }
 
     public void Update ()
     {
 
     }
 }

And this is the state script.

 using UnityEngine;
 using Assets.Code.Interface;
 
 namespace Assets.Code.States
 {
     public class PlayStateScene : IStateBase
     {
         private StateManager manager;
         PlayerDetection playerOnFloor;
 
         void Start()
         {
             playerOnFloor = GameObject.Find("RoomMesh001").GetComponent<PlayerDetection>();
         }
 
         public PlayStateScene (StateManager managerRef)
         {
             manager = managerRef;
             if (Application.loadedLevelName != "Scene 2 Gameplay")
                 Application.LoadLevel ("Scene 2 Gameplay");
         }
 
         public void StateUpdate()
         {
             if (playerOnFloor)
             {
                 manager.SwitchState (new ResultStateScene (manager));
             }
         }
 
         public void ShowIt()
         {
             Debug.Log ("In PlayStateScene1");
         }
     }
 }

I could really do with some help of were to go next. Cheers 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
0

Answer by Baste · Dec 08, 2014 at 01:08 AM

There's several ways to go about this, but for the setup you've got, the easiest would be to just make the PlayerStateScene a singleton, and have it accessible from wherever:

 public class PlayStateScene : IStateBase
 {
     private static PlayStateScene _instance;
     public static PlayStateScene instance {
         get {
             return _instance;
         }
     }


     void Start()
     {
         //assume you only make one. You could make a check
         //and throw an error if _instance's already bound.
         _instance = this;
     }

Then you can simply pass the message along in the collision detection script:

 public void OnCollisionEnter(Collision col)
 {
     if(col.gameObject.name == "bn_spineIK_d001")
     {
         //Send the message to the instance.
         PlayStateScene.instance.RegisterCollision();
     }
 }

You could also create a event/listener system where stuff like the PlayerDetection script registered themselves at some central event listener, but a singleton is quick and gets the job done. It's pretty easy to debug too.

If you want to get fancy, don't instantiate the singleton before it's accessed the first time. Saves you the trouble of thinking about Start/Awake orders and whatnot, and a couple of lines of boring initialization code.

 private static PlayStateScene _instance;
 public static PlayStateScene instance {
     get {
         if(_instance == null)
             _instance = new PlayStateScene();
         return _instance;
     }
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# Script interaction between separate locations 1 Answer

Distribute terrain in zones 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

Unity3d as OPC Client 4 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