Search Unity

How can I create an instantiated prefab WITH serialized field parameters adopted?

Discussion in 'Scripting' started by danmct1995, Jul 11, 2020.

  1. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    I have an object that I want to be instantiated and I want to share the same respawn point as its prefab. When it is instantiated the field for the respawn point is left blank and I'd just like it to be filled in. How could I call a prefab spawned to have that respawn point or assign the instantiated object any value to that area? I'll mark the area below in a picture. The first picture is what I want and the second is the clone, which I need to have the same parameters as the prefab.

    Good.png Bad.png
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If those fields are filled in on the prefab itself, they should be filled in when you instantiate it.

    Open up the prefab itself in the editor. Pretty sure you'll see those fields are blank there.

    The problem that you're likely running into is that prefabs cannot have references to objects in your scene (there's no guarantee that that scene will be open or those objects will exist when a prefab is instantiated).

    If you need references to objects in your scene, then you could consider cloning an object from your scene instead of a prefab. Just keep a disabled copy of the thing around that you can copy whenever you need a new one.

    Alternately, you can set the value of those fields from script right after you call Instantiate.
     
    danmct1995 and Kurt-Dekker like this.
  3. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    I'd ideally like to set the value of the fields, how would I actually go about actually doing that in my script? I've been trying to figure out how to do this for a while now :/ This is what I have for my instantiation so far...

    Code (CSharp):
    1.     public void Multiply()
    2.     {
    3.         Ball powerup = Instantiate(ball, transform.position, transform.rotation) as Ball;
    4.         powerup.GetComponent<Rigidbody2D>().velocity = Random.Range(LowVelocity, HighVelocity) * transform.localScale.x * powerup.transform.right + Random.Range(LowVelocity, HighVelocity) * transform.localScale.y * powerup.transform.up;
    5. powerup.GetComponent<Ball>().RespawnPoint = // somehow reference paddle child as location
    6.     }
     
    Last edited: Jul 11, 2020
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Looks like you already understand roughly how to set the fields of an object when you instantiate it (though if "powerup" is already of type "Ball", then there's no need to call GetComponent<Ball>), and the problem is you don't know the correct value to set them to.

    Lots of ways to get a reference to an object. The simplest way is usually to create a public variable in the script that needs the reference, and then drag the appropriate object to that variable in the inspector. You also might be able to use one of the various FindObject functions.