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 rhianu · Feb 11, 2011 at 06:49 AM · collisionvariablemonobehaviour

Accessing a variable in one script from another script? (using C#)

Okay, I've been trying to figure this out forever but it's not working and it's driving me crazy.

I've got two game objects, and I need the second game object to do something when the player collides with the first game object.

The first game object, which works exactly like it's supposed to, is just a red cube that I've set up to detect collisions from the player using the following script:

collision.cs

using UnityEngine; using System.Collections;

public class collision : MonoBehaviour { public static bool touchedRedCube = false; int i = 0;

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         i++;
         Debug.Log("Touched the cube " + i);
         touchedRedCube = true;
     }
 }

}

Now like I said, that above code works just fine. The problem comes in when I try to have another game object preform an action when the player collides with the red cube. This other game object is just an EmptyGameObject with the following script attached:

test.cs

using UnityEngine; using System.Collections;

public class test : MonoBehaviour {

 // Use this for initialization
 void Start ()
 {

 }

 // Update is called once per frame
 void Update ()
 {
     if (collision.touchedRedCube == true);
     {
         Debug.Log("Preformed an action.");
     }
 }

}

However, instead of printing "Preformed an action" to the debug log only when the player touches the red cube like it should, the above script starts printing the message as soon as the level loads, and continually prints it every single frame. And no, the player is not spawning directly on top of the red cube. I specifically set the scene up with the cube positioned a little ways in front the player so you have to walk to touch it.

What am I doing wrong?

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 poncho · Feb 11, 2011 at 03:12 PM 0
Share

the code seems ok, the only thing that maybe is not working is the OnTriggerEnter, you can try replacing this by void OnCollisionEnter(Collision collisionInfo) or a mouse option with the respective validations, also you can put the code on the update and make a raycast for the collision

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessy · Feb 11, 2011 at 07:19 AM

http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html

Use the Awake or Start function to do initialisation.

What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.

Static variables are especially a pain if you don't listen to that advice.

Personally, I use setup functions triggered by menu items, in the Editor, instead of Awake or Start, but, same idea.

Edit: collision is a bad name for a class, for three reasons. Class names should start with a capital letter, Unity already has a class named Collision, and it is not very descriptive. Rename it what you like, but not collision.

using UnityEngine;

public class DescriptiveName : MonoBehaviour {

public static bool touchedRedCube;

public void Awake () { touchedRedCube = false; }

int i; void OnTriggerEnter (Collider other) { if (other.CompareTag("Player") {
i++; Debug.Log("Touched the cube " + i); touchedRedCube = true; } }

}

.

using UnityEngine;

public class Test : MonoBehaviour {

void Update () { if (DescriptiveName.touchedRedCube) Debug.Log("Performed an action."); }

}

Comment
Add comment · Show 2 · 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 rhianu · Feb 11, 2011 at 09:56 AM 0
Share

I'm very new to coding. Do you have perhaps an example of how to set that up?

avatar image Jessy · Feb 11, 2011 at 04:02 PM 0
Share

I edited it to use Awake(). Explore this page if you want to get an idea of how I do it, and then ask another question if you need more relevant information. http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$enuItem.html

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

No one has followed this question yet.

Related Questions

Editing a variable from another script on collision 3 Answers

What is the problem in my script? 2 Answers

Javascript score problems whilst referencing scripts. 2 Answers

Collision object and access to a script variable? 1 Answer

Accessing variable of script on an instance. 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