Search Unity

New to C#; Programmatically add gameObjects & Components?

Discussion in 'Scripting' started by Gen0LovesYou, Oct 18, 2017.

  1. Gen0LovesYou

    Gen0LovesYou

    Joined:
    Oct 18, 2017
    Posts:
    1
    [Greasemonkey cleanup]
     
    Last edited: Oct 19, 2017
  2. ezjm427

    ezjm427

    Joined:
    May 17, 2014
    Posts:
    100
    GameObjects must be created with the Instantiate function using a reference to a prefab or a reference to another existing GameObject to copy., not 'new GameObject'. I'm not sure how it will work if you don't use Instantiate.

    You can create components and stuff through code, but sometimes it has undesirable effects, it's usually better to make prefabs of GameObjects that have the settings you want them to have, and then Instantiate them using a reference in code.

    One of the colliders (either the source or target collider) object has to have a rigidbody attached for OnTrigger functions to work. It's a dumb restriction but it won't do anything without it. Make sure to change the Rigidbody motion to Kinematic

    Then also make sure the source object has Collider set to Trigger in the Unity Editor.