2

I have a simple project where I spawn multiple asteroids and they tend to gravitate over a planet. I wanted to add a simple trail to enhance the visual effects. It's very simple when I add the asteroid manually and then add component "trail renderer" and select desired material. But I can't work it out on how to add it to a script. This is my code for now:

using System.Collections;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(FauxGravityBody))]
public class Spawner : MonoBehaviour {

    public GameObject meteorPrefab;
    public float distance = 20f;
    public float time = 10f;
    private GameObject meteor;
    public TrailRenderer trail;
    void Start ()
    {
        StartCoroutine(SpawnMeteor());
    }

    IEnumerator SpawnMeteor()
    {
        Vector3 pos = Random.onUnitSphere * distance;
        meteor = Instantiate(meteorPrefab, pos, Quaternion.identity);
        meteor.AddComponent<FauxGravityBody>();
        meteor.AddComponent<DestroyMeteor>();
        meteor.AddComponent<SphereCollider>();
        meteor.AddComponent<TrailRenderer>();

        yield return new WaitForSeconds(time);

        StartCoroutine(SpawnMeteor());
    }

}

Which indeed adds the trail to the spawned objects, but it's using the default pink trail. My desired trail material is located in folder "Assets/Effects/Trail.mat". How can I specify in the script that I want to use that specific material?

Kind regards.

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • 1
    Note if you need these components -- TrailRenderer, SphereCollider etc. -- on all meteors, you can simply add them to the Prefab itself manually (by dragging them over it). Then they will already be there, right materials and all, once you Instantiate the main prefab. You can then still adjust specific settings (or disable components) by using getting them via `GetComponent()` etc. Good luck! – Philipp Lenssen Feb 16 '20 at 12:27
  • 1
    @PhilippLenssen This should be the answer. – mrogal.ski Feb 16 '20 at 12:37
  • @Mateusz Done, thanks. I wasn't 100% sure if the asker had some hidden intention behind script-adding components, but I guess the chances aren't high. – Philipp Lenssen Feb 16 '20 at 12:41
  • This is about [tag:c#], not [tag:unityscript]. – Ruzihm Feb 26 '20 at 19:10

2 Answers2

1

Note if you need these components -- TrailRenderer, SphereCollider etc. -- on all meteors, you can simply add them to the Prefab itself manually (by dragging them over it). Then they will already be there, right materials and all, once you Instantiate the main prefab.

You can then still adjust specific settings (or disable components) by using getting them via GetComponent<TrailRenderer>() etc. Good luck!

Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77
0

The best way to do it is....

create Prefab and attach you needed component to your Prefab

and Instatiate it when and where you need it

and using GetComponent<>() method you can get attached component

GameObject bullets = Instantiate(yourPrefab, yourtransform.position, Quaternion.identity) as GameObject;
bullets.GetComponent<Rigidbody>().AddForce(transform.forward * 100, ForceMode.Impulse);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Jaydeep chatrola
  • 2,423
  • 11
  • 17