Search Unity

Collision detection and variable values import

Discussion in 'Scripting' started by Tio_Rafinha, Mar 28, 2013.

  1. Tio_Rafinha

    Tio_Rafinha

    Joined:
    Feb 7, 2013
    Posts:
    3
    Good evening, I'm new to unity, and have to do something like this:

    I have a plane and a random object that fall into that plane, this object have atached to it a script with a variable. What I want to to do is, when the object fall on the plane, the plane will identify the object, access the script and copy the value of the variable.

    Sorry by the bad english =D

    thanks.
     
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I'm kind of new to programming myself, but maybe you could try this:

    Attach a collider to your plane. Then make the collider a trigger by checking the "Trigger" box on your collider component.

    Now, attach a script to your plane that calls the "OnTriggerEnter()" function. Then you write some code that says if the falling object enters the plane's trigger, then execute some code.

    For a beginner, learning how to communicate between scripts can be difficult, especially when you're not familiar with the system. But once you learn how, it's really easy. Basically you have to make the variable "public". Then in your other scripts you can access other variables via the GameObject.Find and the GetComponent(otherscript) syntax.
     
  3. Tio_Rafinha

    Tio_Rafinha

    Joined:
    Feb 7, 2013
    Posts:
    3
    Tanks velo, I'll try that.
     
  4. Tio_Rafinha

    Tio_Rafinha

    Joined:
    Feb 7, 2013
    Posts:
    3
    well, thanks for the help with the OnTriggerEnter() I could go really further, but I find another stone on my way...

    I have two Jscripts and I want to pull the value of ones variable to the other one

    for example:

    Script1 - vermelho.js

    public var status=false;

    if (something happen){
    status = true;
    }

    --

    Script 2 - calculo

    var vermeli : vermelho;
    var estado = vermeli.status;

    if (estado==true){
    do something;
    }

    but this isn't working :( I've looked a lot and tryed many things but it just don't work, no matter what I do the variable estado isn't able to access the value from the var status on the first script.

    any thoughs??

    thanks