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

EventTrigger to prefab problem

Discussion in 'UGUI & TextMesh Pro' started by ReniTheGhost, Dec 8, 2014.

  1. ReniTheGhost

    ReniTheGhost

    Joined:
    Dec 8, 2014
    Posts:
    16
    When i try to Apply my UI EmptyObject with EventTrigger PointerEnter and PointerExit to Prefab the Prefab simply forget my setting. If i try to setting up within the Prefab GameObject settings not showed correctly. I can only select Textures/Folders/Materials etc. but i cant select GameObjects.

    Can i set up EventTrigger from js ? How can i?
     
  2. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    same here when use prefab any solution please ?
     
  3. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Is it the prefab under the project folder your saying loses reference? If so its likely because you're setting up references to instances which when converted to a prefab get lost as the instance might not exist.
     
  4. ReniTheGhost

    ReniTheGhost

    Joined:
    Dec 8, 2014
    Posts:
    16


    Yes but another data(yellow circle) not lost. The red circle datas try to connect to the Main Camera. If these datas lost becouse its another object how can i set main camera later? Can i write a script which make a "connection" PointEnter to Main camera in runtime?
     
  5. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Right so yes the data in the yellow circle is part of the prefab asset that is created (as its on the same object). This mean unity can maintain a reference to it as its relative to itself. The others are lost due to them being only part of the scene. There is no way for an asset to know about that instance as it could be loaded into a different scene. (its just how unity has always worked)

    And yes you can make a script that will assign it at runtime. you'll want to look at adding a new Entry to the EventTrigger.delegates list.
     
    ReniTheGhost likes this.
  6. ReniTheGhost

    ReniTheGhost

    Joined:
    Dec 8, 2014
    Posts:
    16
    Can you give me a little advise what i'm need to write into a script? I can only add a new EventTriggerType but i dont know how can i add which gameobject and scipt attach to it.

    Code (JavaScript):
    1. var Entry : EventSystems.EventTrigger.Entry = new EventSystems.EventTrigger.Entry();
    2. Entry.eventID = EventSystems.EventTriggerType.PointerEnter;
    3. (transform.GetComponent("EventTrigger") as EventSystems.EventTrigger).delegates.Add(Entry) ;
    Alredy tryed this but not worked:
    Code (JavaScript):
    1. var Entry : EventSystems.EventTrigger.Entry = new EventSystems.EventTrigger.Entry();
    2.     Entry.eventID = EventSystems.EventTriggerType.PointerEnter;
    3.     Entry.callback = new EventSystems.EventTrigger.TriggerEvent();
    4.     var call : UnityEngine.Events.UnityAction = new UnityEngine.Events.UnityAction( OnLineTrue);
    5.     Entry.callback.AddListener(call);
    6.    
    7.     (transform.GetComponent("EventTrigger") as EventSystems.EventTrigger).delegates.Add(Entry) ;
     
  7. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    let me take a pass at doing it myself (going to add the helper function for everyone) then get back to you with the solution.
     
  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Sorry i've been really busy and havn't gotten to this yet.
     
  9. ReniTheGhost

    ReniTheGhost

    Joined:
    Dec 8, 2014
    Posts:
    16
    Ok not problem ,i found a very easy way to avoid this problem.Not realy nice but working.So you dont need to worry about this, i can upgrade my program later.
    Thank you for telling me.
     
  10. Suguma

    Suguma

    Joined:
    May 29, 2015
    Posts:
    26
    Hey, I came across this and I'm not sure I get the "its just how unity has always worked" part @phil-Unity mentioned.
    I have basically the same issue @HunStix showed here. My instances of a prefab with EventTrigger component lose reference to script and method to call. I get that, in the case of the image above, the instance was reset to the prefab, but that does seem weird, because it only happens with EventTrigger component, as far as I can tell. Other components such as scripts do not get reset (in my case, it is the same prefab and the same instance that loses EventTrigger, but not other script references).

    Does anyone have a take on this? Am I getting this wrong?

    Edit: Adding screenshots of what I mean (sorry for not adding the circles and stuff)

    This is my prefab. It has a script and the Event Trigger components.
    Screen Shot 2015-11-09 at 1.46.23 PM.png
    This is my object, instance of the prefab above. It is exactly the case mentioned before, in which the event trigger loses reference I assigned manually before. What I don't get is why only the EventTrigger component loses reference, when the DualButton component keeps them all, even the ones specific to the instance (the Button Pressed variable bellow).
    Screen Shot 2015-11-09 at 1.46.31 PM.png

    Again, am I getting this wrong?
     
    Last edited: Nov 9, 2015
  11. _OK_

    _OK_

    Joined:
    Oct 3, 2014
    Posts:
    33
    i just ran into this problem. If you set up your canvas and event triggers in the inspector and need them in a prefab - create an empty game object. add the canvas and player and create a prefab from that - it keeps all the event triggers set in the inspector in place u can then unparent the canvas if necessary.