Search Unity

Dragging a Self-Destructive Object into the Scene

Discussion in 'Scripting' started by alexander_q, Sep 10, 2014.

  1. alexander_q

    alexander_q

    Joined:
    Feb 7, 2014
    Posts:
    32
    What I'm trying to accomplish: I have made a method for an object that checks to see if it existed during the level load, and if not, network instantiates a copy of itself and destroys the original. This means that the same object with a network view can either exist from the start, or be added during runtime and function essentially the same. I came up with this because adding objects with a network view during runtime didn't notify connected clients.

    The script is as follows:

    Code (CSharp):
    1.     public bool HereFromTheStart = false;
    2.     public bool NetworkInstantiated = false;
    3.     public string resourcePath = "carrot man resource";
    4.  
    5.     void Start () {
    6.         if(networkView.isMine && !NetworkInstantiated){
    7.             if(!HereFromTheStart){
    8.                 GameObject netMake = (GameObject)Network.Instantiate(Resources.Load(resourcePath,typeof(GameObject)),transform.position,Quaternion.identity,0);
    9.                 netMake.GetComponent<carrotManPace>().NetworkInstantiated = true;
    10.                 GameObject.Destroy(gameObject);
    11.             }
    12.         }
    13.     }
    14.  
    15.     void OnLevelWasLoaded(){
    16.         HereFromTheStart = true;
    17.     }
    The Problem: When dragging the object with this attached, the start function is run as soon as the object is in the scene, instead of when the mouse button is released as would make more sense. This means that the object is immediately destroyed and also immediately instantiates a copy of itself. This causes the drag action to grab another prefab and repeat, which results in a prefab-painting like effect.

    How can I have the start function only run when I release the mouse button while dragging? Or is there another way to solve this?

    What I've tried: Having the code run on mouse-button-up. Unfortunately this detects the mouse up only from the game, not from the scene editor, so it requires me to go between both and click the mouse to have the code run. This is not optimal.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I suppose you have it executing in the editor?
    You can poll Application.isPlaying to check if you're already playing and not do the destruction then.
     
  3. alexander_q

    alexander_q

    Joined:
    Feb 7, 2014
    Posts:
    32
    I want it to happen while playing - I'm throwing prefabs into a running network game.
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I see.
    Instead of relying on the editors drag and drop, one workaround would be to make an editorscript that spawns what you want where you click in the sceneview. Another would be to do the same in the gameview.