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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

onMouseOver not returning correct GameObject instance

Discussion in 'Scripting' started by emicenizo, Mar 26, 2016.

  1. emicenizo

    emicenizo

    Joined:
    Mar 26, 2016
    Posts:
    7
    Hi there!

    So, I'm creating an MonoBehaviur object like this:

    Code (CSharp):
    1. GameObject dibujo = navesGO;
    2. Nave nave = ScriptableObject.CreateInstance("Nave") as Nave;
    3.         nave.initNave(dibujo, nombre);
    4. return nave;
    navesGO has the prefab that is being instanciated.

    Then, I instanciate it on the scene like this:

    Code (CSharp):
    1. GameObject itemNuevo = Instantiate(nave.getGameObject(), randomVectorPos, Quaternion.identity) as GameObject;
    2.  
    This correcly inserts the GameObject on my scene.

    My Nave Object has the onMoouseOver method overrided. A simple thing like this:

    Code (CSharp):
    1.     void OnMouseOver() {
    2.         Debug.Log(this);
    3.     }
    4.  
    It works... the problem is that the thing that is logged is not the object I created early, is a new and empty object. Its like the GameObject that is instnaciated is different from the one in the scene.

    Any insights?

    Thanks in advance!
     
  2. ILemonDev

    ILemonDev

    Joined:
    Dec 23, 2015
    Posts:
    11
    This is what i would do:

    I would attach a script to the navesGO prefab. his script would contain the OnMouseOverCode(). You would assign this prefab to dibujo. Then just instantiate that object as so:

    Code (CSharp):
    1. GameObject itemNuevo = Instantiate(dibujo, randomVectorPos, Quaternion.identity) as GameObject;
    2.  
    That would give you back your correct object. I don't know why you return the gameobject in the way you do.