- Home /
Question by
DenizCetinalp · Feb 26, 2014 at 06:31 PM ·
c#javascriptgameobjectobjectcast
javascript to C#, UnityEngine.Object to GameObject?
I am learning C# and unity, but im following a tutorial that is using javascript. I got stuck here:
var newParticles = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
newParticles.GetComponent(ParticleAnimator).autodestruct = true;
I was reading the documents, Instantiate returns a UnityEngine.Object. So I tried UnityEngine.Object newParticles = Instantiate()... This gives me an error in the last statement because newParticles is not a GameObject, so I cant use GetComponent() with it. I looked up GetComponent, which returns a Component, and a Component is a child of UnityEngine.Object. Why do I have to cast newParticles as a GameObject first?
Here is what I did:
GameObject newParticles = (GameObject)Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
newParticles.GetComponent<ParticleAnimator>().autodestruct = true;
Comment
Best Answer
Answer by perchik · Feb 26, 2014 at 07:31 PM
Perhaps try
GameObject newParticles = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;