Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by WojciechBraszkiewicz · Feb 16, 2020 at 01:20 PM · scripting problemgameobjectspawnspawning

Spawn an object with additional parameter

Hello,

I just wanted to start my first project in Unity.

What I wanted to achieve is a simple game, where you move around the sphere and dodge obstacles falling from the sky.


Now I have a script that adds gravity to an object (what it does is that an object is getting pulled by my planet (sphere) called attractor).

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody))]
 public class FauxGravityBody : MonoBehaviour {
 
     private FauxGravityAttractor attractor;
     private Rigidbody rb;
 
     public bool placeOnSurface = false;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         attractor = FauxGravityAttractor.instance;
     }
     
     void FixedUpdate ()
     {
         if (placeOnSurface)
             attractor.PlaceOnSurface(rb);
         else
             attractor.Attract(rb);
     }
 
 }


And I also have a script that randomly spawns objects:

 using System.Collections;
 using UnityEngine;
 [RequireComponent(typeof(FauxGravityBody))]
 public class Spawner : MonoBehaviour {
 
 
     public GameObject meteorPrefab;
     public float distance = 20f;
     void Start ()
     {
         StartCoroutine(SpawnMeteor());
     }
 
     IEnumerator SpawnMeteor()
     {
         Vector3 pos = Random.onUnitSphere * 60f;
         Instantiate(meteorPrefab, pos, Quaternion.identity);
 
         yield return new WaitForSeconds(1f);
 
         StartCoroutine(SpawnMeteor());
     }
 
 }



Now, while each spawned object requieres "FauxGravityBody" attribute - it doesnt spawn with it.

So basically what I want to achieve is that each spawned object on default has "FauxGravityBody" attribute attached to it.

What would be the best way to achieve this?

Kind regards.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Feb 17, 2020 at 12:35 AM

The 'Instantiate' function actually returns a reference to the object that has just been created. We can use that reference to add new components to the object directly;

 GameObject go = Instantiate(meteorPrefab, pos, Quaternion.identity);
 go.AddComponent<FauxGravityBody>();

Similarly, if you needed immediate access to that new component, the 'AddComponent' function also returns a reference to the component that was added.

However, because you are using a prefab for instantiation, all of this is unnecessary. If you want new objects to spawn with that component added, all you need to do is add the component directly to the prefab itself in the editor.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

266 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if a position is occupied in 2D? 1 Answer

Script causing lag 1 Answer

How do I get an object to Instantiate infront of a specific object/player? 1 Answer

Help? Im trying to get my zombie prefab to spawn, but stop them spawning after a set spawn limit. 1 Answer

Why are these object passing through each other? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges