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 AAcat · May 19, 2015 at 08:58 AM · javascriptconnection

2 Triggers, 2 scripts, need help communicating the two.

okay so i have 2 triggers (game objects). when one is touched by the player it sets a variable "secret" to 1. the second trigger ask "if touching the player and if secret == 1 then set secret to 2". sounds simple right? well i keep getting errors because i have no clue how to connect the two script from two different game objects. I have searched the far corners of the internet and still can't find a solution that is probably vary simple and I'm just probably too blind to know. here is my code:

 #pragma strict
 public var secret : int = 0;
 
 function OnTriggerEnter(col : Collider) {
     if(col.tag == "Player")
     {
     secret = 1;
     }
 }

and

 #pragma strict
 private var otherScript : Secret1;
 
 function Start () {
     otherScript = GetComponent (Secret1);
 }
 
 function OnTriggerEnter(col : Collider){
     if(col.tag == "Player")
     {
     
         if (otherScript.secret == 1) //the problem is here. The two scripts are not in the same game object so i need a solution to substitute this.
             {
             otherScript.secret = 2;
             }    
     }
 }


I need to know the best way to connect the two.

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 Nerevar · May 19, 2015 at 09:15 AM 0
Share

why do you need two scripts? couldn't you have just one? cause it seems that regarding the way you access "Secret1" they are on the same gameObject.

Try to explain further your situation and what you are trying to do because this might not be the optimal way to do it :)

This being said and If it is compatible within your scene, you could just use a public var otherScript : Secret1 and assign it from the inspector before entering playmode.

avatar image Halfbiscuit · May 19, 2015 at 09:16 AM 0
Share

Well I don't know JS but In c# you can make the 'otherScript' variable public and then drag the script you want to connect onto the variable in the editor.

Otherwise you can try GameObject.Find.

avatar image AAcat · May 19, 2015 at 11:33 AM 0
Share

It's not the same game object @Nerever and I already tried GameObject.Find @Halfbiscuit. :/

1 Reply

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

Answer by Landern · May 19, 2015 at 01:57 PM

If it isn't on the same game object then finding the component will leave you sad. To use GetComponent without the gameobject reference will leave the GetComponent method searching on the game object that the executing script is attached to.

I've modified your scripts a little to have it search for the GameObject that i've attached Secret1 on, I did change the case of your script as a FYI. I also used the generic type of GetComponent to ensure i'm not doing additional casting from type Component to the desired type.

 //OtherScript.js
 #pragma strict
  private var otherScript : Secret1;
  
  function Start () {
      otherScript = GameObject.Find("Secret1Object").GetComponent.<Secret1>();
      if (otherScript) {
          Debug.Log("Found the other script by it's name");
          Debug.Log("OtherScript secret value: " + otherScript.secret);
      }
          
  }
  
  function OnTriggerEnter(col : Collider){
      if(col.tag == "Player")
      {
      
          if (otherScript.secret == 1) //the problem is here. The two scripts are not in the same game object so i need a solution to substitute this.
          {
              otherScript.secret = 2;
          }    
      }
 
  }
 
 //Secret1.js
 #pragma strict
 public var secret : int = 0;
  
 function OnTriggerEnter(col : Collider) {
     if(col.tag == "Player")
     {
         secret = 1;
     }
 }

You can see from the screenshot that the execution with GameObject.Find was fruitful and found the GameObject called "Secret1Object" that has the Secret1.js script attached to it.

2 Triggers, 2 scripts, need help communicating the two

There are other times when you may need to get components in the GameObject heirarchy as well, there are methods for that as well such as GetComponentsInChildren and GetComponentsInParent, these are the plural versions and there are singlular versions that return the first occurance of a component of a particular type. You should read the documentation for the Component type.

Lastly, don't ask for help and state you get errors and then not post them, that silly and generally wastes everyone's time asking you what the error is. Luckily this is such a common question and stating "I have searched the far corners of the internet and still can't find a solution" is very frustrating given there are thousands of posts asking this same question.


2-triggers-2-scripts-need-help-communicating-the-t.jpg (219.1 kB)
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 AAcat · May 19, 2015 at 03:54 PM 0
Share

I was kidding about the "corners of the internet" part, anyhoo, I appreciate the answer, however the "will leave you sad" part seemed a bit odd.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

JavaScript TcpClient().Connect freezes 1 Answer

Find connections dont working 0 Answers

Connection between 2 scripts 1 Answer

MasterServer errors 1 Answer

Internal error while attempting to connect to master server 0 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