Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

does every gameobject create a different "instance" of the script component it is using?

Discussion in 'Scripting' started by diekus, Jun 17, 2014.

  1. diekus

    diekus

    Joined:
    Jan 16, 2013
    Posts:
    10
    Hello,

    I have a basic question regarding components. Let's say I create a script that I want to put to different elements in the scene. This script uses RaycastHits to detect when the mouse is over a sprite. When the mouse is over, then the element moves along the Z axis. When the mouses leaves the element, it returns to it´s original position.

    I am having an issue because when I have more than one element in the scene whenever i hover the mouse it does move in the Z axis, but it stops the movement of the other elements that use the same script in the scene.

    So I guess, the question is,

    does every gameobject create a different "instance" of the component it is using? I would guess it´s a yes, but It surely seems weird that hovering over one element causes the other to stop their movement. Could this be becaus eof the raycasthit?
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Yes, each script is it's own instance. The first thing that comes to mind is maybe you're using a "static" variable somewhere to control the speed?
     
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Post your code.
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    As long as we're not talking about static 'variables', yes.
     
  5. diekus

    diekus

    Joined:
    Jan 16, 2013
    Posts:
    10
    Hi again

    Here's the code: It's only the update function (you'lll see two raycasthits cause it's a side by side stereo config), but I don't know why when the mouse is over one sprite with this script as component the other sprites stop moving until the mouse is in a place where it hits none.

    Code (CSharp):
    1. private RaycastHit hit;
    2. public float popOutSpeed = 2f;
    3.  
    4. void Update () {
    5. if (Physics.Raycast(refLeftCam.ScreenPointToRay(Input.mousePosition), out hit) || Physics.Raycast(refRightCam.ScreenPointToRay(Input.mousePosition), out hit))
    6. {
    7. if (hit.transform.position.z <= (float)StereoParams.depthLayers.onScreen + 0.1 && hit.transform.position.z > (float)StereoParams.depthLayers.middleUserSpace)
    8. {
    9. hit.transform.position = Vector3.Lerp(hit.transform.position, new Vector3(hit.transform.position.x, hit.transform.position.y, hit.transform.position.z - 1), popOutSpeed*Time.deltaTime);            
    10. }
    11. }
    12. else // mouse isnt over
    13. {
    14. if (transform.position.z <= (float)StereoParams.depthLayerArray[2])
    15. this.transform.position = Vector3.Lerp(transform.position, new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z + 1), popOutSpeed*Time.deltaTime);
    16. }
    17. }
     
    Last edited: Jul 22, 2014
  6. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Do you have this attached to many sprites? :eek:
     
  7. diekus

    diekus

    Joined:
    Jan 16, 2013
    Posts:
    10
    UPDATE: ok, so now i find out about OnMouseOver and OnMouseExit... I'll test it and see how does it work.

    I have this attached to 5 sprites! why?! :oops:

    I'm guessing the mouse hover implementation is not a good one? XD
     
    Last edited: Jun 18, 2014
  8. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Because it explains everything.

    (Physics.Raycast(refLeftCam.ScreenPointToRay(Input.mousePosition), out hit) || Physics.Raycast(refRightCam.ScreenPointToRay(Input.mousePosition), out hit))

    This condition is true always if one of these rays hit an object. And if condition is true, then else block isn't working. This is the same for all objects "wearing" this script. This makes only the object "hit" moving as long as condition is true.
     
  9. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    I suggest that you replace
    else
    with
    if (hit.gameObject != this.gameObject)


    P.S. But I don't like ScreenPointToRay to be executed with many objects as you probably noticed :p
     
  10. diekus

    diekus

    Joined:
    Jan 16, 2013
    Posts:
    10
    I made some changes, to make things easier. But I have a related problem. I'm using OnMouseOver and OnMouseLeave to activate a PopOut and a PushIn function that only change the gameObject's z position.

    The thing is, If I want this for selection purposes, it still acts as if all instances had the same script. There's nothing static, but I it might have to do with mouseOver somehow using a ray that touches several other colliders?

    There's no hit anymore, so its unlikely? I've just being trying several ways to do this -very simple selection stuff- and can't seem to get around it.

    Code (CSharp):
    1. public bool selectionModeOn = false;
    2.     private bool isSelected = false;
    3.  
    4.     private RaycastHit hit;
    5.     public float popOutSpeed = 10f;
    6.     public float popAmount = 3f;
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.         if(Input.GetMouseButtonDown(0))
    11.         {
    12.             if (selectionModeOn)
    13.                 this.isSelected = !this.isSelected;
    14.         }
    15.     }
    16.  
    17.  
    18.     void OnMouseOver()
    19.     {
    20.         PopOut(transform);
    21.     }
    22.  
    23.     void OnMouseExit()
    24.     {
    25.         if (!selectionModeOn || selectionModeOn && !isSelected)
    26.             PushIn(transform);
    27.     }
    28.  
    29.     private void PopOut(Transform elemToPop, bool digital = true)
    30.     {
    31.             elemToPop.position = Vector3.Lerp(elemToPop.position, new Vector3(elemToPop.position.x, elemToPop.position.y,  -popAmount), popOutSpeed * Time.deltaTime);
    32.     }
    33.  
    34.     private void PushIn(Transform elemToPush, bool digital = true)
    35.     {
    36.             elemToPush.position = Vector3.Lerp(elemToPush.position, new Vector3(elemToPush.position.x, elemToPush.position.y, popAmount), popOutSpeed * Time.deltaTime);
    37.     }
    Why do all tiles behave as a batch if every gameObject has its own script with MouseOver, MouseDOwn and MouseExit functions??
     
  11. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Check this out. It's en example of mouse events usage.
     

    Attached Files:

    diekus likes this.