Search Unity

On collision/Trigger change prefab

Discussion in 'Scripting' started by ostrich160, Jun 14, 2014.

  1. ostrich160


    Feb 28, 2012
    Hi guys, this is quite a complicated one, or at least I think it is. Basically, I want to make it so on collision or trigger with an object, my player changes into another prefab. Its basically that the player becomes a different player that I have made, if you know what I mean. Is there a way to do this

    Thanks again
  2. crag


    Dec 13, 2013
    I am sure there are many ways to do this but here is a thought:

    Create a "SharedObject" (game object with storage script component added, "MyScript.js" in this example; player prefs would work too) to store the important information about the first instance of the player (lets call it PlayerA). Then, when the OnTriggerEnter occurs, store (set) the data you want share with PlayerB when you Instantiate it. For instance, all the important Transform information: parent, localPosition, localEulers... and also some game/player specific data: health, status, whatever.

    For instance:

    var sharedObject = GameObject.Find("SharedObject"); // check out DontDestroyOnLoad(transform.gameObject);

    var sharedScript = sharedObject.GetComponent("MyScript"); // path to getters and setters for storage and retrieval
    sharedScript.storeData(player.transform.localPosition, playerHealth, playerType); // you don't have to shove everything into one setter, it could be as many or few as you need

    Then, destroy PlayerA:
    Destroy(playerCollider.gameObject); // all of this is assuming the OnTriggerEnter resides in the object that hit PlayerA (a comet, a tulip, another player, etc)

    Then, immediately after, create PlayerB and tell it where to be and what to know:

    // place the new prefab (prefab could be assigned in the editor or grabbed from a list?)

    var newPosition = sharedScript.getPosition(); // a vector3 containing local position maybe
    var newScale = sharedScript.getScale(); // a vector3 containing localScale
    var newParent = sharedScript.getParent(); // a game object to stick this prefab in
    var prefabObject = Instantiate (prefab, newPosition, Quaternion.identity); = "Yasiin Bey " + (prefabCounter + 1); // maybe there will be bunches of the same kind?

    prefabObject.transform.parent = newParent.transform; // add to PlayerA's parent
    prefabObject.transform.localScale = newScale; // match scale?
    // possibilities are endless...

    I hope this is helpful. I'm sure some of the seasoned masters/gurus will serve up even more useful tips than this one!
    Last edited: Jun 14, 2014
  3. MTDues


    Mar 27, 2014
    so is this a script i can use?
  4. cstooch


    Apr 16, 2014
    This shouldn't be anything too complicated actually, unless I'm misunderstanding your needs here (which is possible, since you didn't provide much detail yet).

    You could possibly do this with one prefab for your character. You would have an empty gameobject act as a container. You can even keep all your scripts attached to this, so that it's easy to maintain all the variables that you need, like health and whatever might be pertinent. Below it you have the different possible characters as children of this game object. Really, they're just the visual implementation of your character. You enable / disable them as required. If the model is the same, but the "skin" is different, then the solution for that is even a little simpler.

    Would that work for what you need? If not, then it really depends on your needs which you might have to elaborate on more, like are any variables about the character going to be completely different.. different methods, etc... something that would be drastically different between the characters that you would have a tough time maintaining in one central spot like I mentioned above.

    EDIT: Damn, just realized the poster above necroed an old thread.
    Last edited: Aug 2, 2017