Search Unity

SendMessage after Instantiation

Discussion in 'Scripting' started by Sollthar, Dec 13, 2010.

Thread Status:
Not open for further replies.
  1. Sollthar

    Sollthar

    Joined:
    Feb 11, 2010
    Posts:
    486
    How would you go about sending a message to an object that has just been instantiated? For example, I want to instantiate a bullet with a script with a "strength" value attached to it. But I want the strength value to be edited by the script of the object firing it so I can use the same bullet object but with different strengths.

    What I did was this:

    Instantiate(bullet,transform.position,transform.rotation);bullet.SendMessage("SetStrength", 200.0);

    But it doesn't seem to work - and I think I know why. What I don't know is, how to make it work.


    Any help would be greatly welcomed, thank you!
     
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    var tempBullet : gameObject = Instantiate(bullet,transform.position,transform.rotation);

    tempBullet.SendMessage("SetStrength", 200.0);

    Syntax may be incorrect, there may be more efficient ways of doing this.
     
  3. Sollthar

    Sollthar

    Joined:
    Feb 11, 2010
    Posts:
    486
    You're my hero. Thank you! That worked!
     
Thread Status:
Not open for further replies.