Search Unity

New Objects On Mouse Over

Discussion in 'Scripting' started by squishypanda, Nov 29, 2012.

  1. squishypanda

    squishypanda

    Joined:
    Nov 16, 2012
    Posts:
    5
    Hi therIs there some sort of code that is able to create 5 new objects when a mouse hover over it? I have used an instantiate code before but that only clones when I press the key assigned which is "e". Basically I have plants that are deadly but i want the user when scrolled over one of the plants have 5 gold coins appear as a reward I am using java script by the way =)

    If it's not too much to ask, how do you get a character to respwan your player after a certain amount of time? :)

    Thank you
     
  2. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    Show us the code you have.
    Instantiate should work for you.
    • Make a prefab of the coin.
    • Make a member of your plant class to hold the prefab.
    • Assign it in the inspector.
    • Define the function "OnMouseEnter" in your plant script
    • In OnMouseEnter Call instantiate 5 times on the prefab member with desired position and rotation.
    The name of the objects will say clone. That is essentially what you are doing when you are instantiating.
    Why is cloning a bad thing for you?
     
    Last edited: Nov 29, 2012
  3. squishypanda

    squishypanda

    Joined:
    Nov 16, 2012
    Posts:
    5
    Hi! Thank you very much for replying! My code is as follows just now but instead of the getkey e as shown below I just need it to be hovered over the tree and 5 coins appear =)

    var newObject : Transform;
    function Update () {
    if (Input.GetKey("e")) {
    Instantiate(newObject,transform.position, transform.rotation);
    }
    }
     
  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Check out you doc under runtime classes MonoBehaviour to see all the built-in function types with examples.

    Then you can use a loop or five lines for instantiating. Also Invoke Repeat may be a good way to pump out five coins. Remember that if they have colliders they will collide with each other and shoot off in different directions lol.
     
  5. squishypanda

    squishypanda

    Joined:
    Nov 16, 2012
    Posts:
    5
    you mean the doc as in on the Unity Script Reference website? =) Also thank you ^^
    Lol I will, the code I had above was just weird as it kept sprouting out trees every time i pressed "e" and it ended up killing itself cos the trees are meant to be deadly haha!