Search Unity

How can I make this script work?

Discussion in 'Scripting' started by Stef_Morojna, May 24, 2015.

  1. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    I have this script
    Code (CSharp):
    1. GameObject des = (GameObject) Instantiate(blackParticle, new vector3 (0,0,0), Quaternion.identity);
    2.             Destroy ( des, 0.2f);
    It gives me this error:
    Cannot cast from source type to destination type.
    I tryed a few things and searched around the forum but nothing really worked.
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    Casting needs the variable your assigning to be the same as what you instantiate - try using "var des" without "(GameObject)" instead of "GameObject des". Maybe that will work. I could be wrong though haha.

    Is blackParticle a prefab gameobject?
     
    Stef_Morojna likes this.
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Instead of
    Code (csharp):
    1. new vector3 (0,0,0)
    it should be
    Code (csharp):
    1. new Vector3 (0,0,0)
    or
    Code (csharp):
    1. Vector3.zero
     
    Stef_Morojna likes this.
  4. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Its a prefab particle effect

    Edit: I tryed a few more things and still didnt get it to work

    Code (CSharp):
    1. var des = (GameObject) Instantiate(blackParticle, new Vector3 (0,0,0), Quaternion.identity);
    2.             Destroy ( des, 0.2f);
    This gives me a error saying "cant destroy a transform"

    Then I tryed this. It dosent give me a error but it still dosent work.
    Code (CSharp):
    1. var des = (GameObject) Instantiate(blackParticle, new Vector3 (0,0,0), Quaternion.identity);
    2.             Destroy ( des.gameObject, 0.2f);
     
    Last edited: May 24, 2015
  5. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    I actually had a pretty long code in there but I just replaced it with new Vector(0,0,0) so I dont waste other peoples time...
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you do that, in your code, it wouldn't compile. Be careful when you post something else than you have in your actual code!

    Could you show the declaration for blackParticle?
     
    Stef_Morojna likes this.
  7. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    What do you mean with "declaration for blackParticle"?
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It would be best to just show the complete script.
     
    Stef_Morojna likes this.
  9. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    The SpriteRenderer dosen't show in blue, probably because the forum code isnt updated. :p

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Body : MonoBehaviour {
    5.  
    6.     public bool canRespawn;
    7.     public Transform blackPartic;
    8.     private SpriteRenderer sprt;
    9.  
    10.     void Start() {
    11.         sprt = GetComponent<SpriteRenderer>();
    12.     }
    13.  
    14.     void OnTriggerEnter2D(Collider2D coll) {
    15.         if (coll.gameObject.tag == "Obstacles") {
    16.             sprt.color = Color.clear;
    17.             Invoke("RespawnReady", 1);
    18.             GetComponent<PolygonCollider2D>().offset = new Vector2(0,50);
    19.         }
    20.  
    21.         if (coll.gameObject.tag == "Bugs") {
    22.             Destroy (coll.gameObject);
    23.             var effect =  Instantiate (blackPartic, coll.gameObject.transform.position, Quaternion.identity);
    24.             Destroy ( effect, 0.2f);
    25.         }
    26.     }
    27.  
    28.     void RespawnReady () {
    29.         canRespawn = true;
    30.     }
    31.  
    32.     void Respawn () {
    33.         canRespawn = false;
    34.         sprt.color = Color.white;
    35.         Invoke("ImmuneOff", 1);
    36.     }
    37.  
    38.     void ImmuneOff () {
    39.         GetComponent<PolygonCollider2D>().offset = new Vector2(0,0);
    40.     }
    41.  
    42.     void Update () {
    43.         if (Input.GetMouseButtonDown(0)) {
    44.             if (canRespawn == true) {
    45.                 Respawn ();
    46.             }
    47.         }
    48.     }
    49. }
     
    Last edited: May 24, 2015
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    At line 7, you declare that blackPartic is a Transform. That means, you can only cast it to a Transform at line 23. You originally had it declared to be a Transform and then you made a cast to a GameObject. That doesn't work. Both need to be identical. It doesn't matter whether you use GameObject or Transform, but make sure it is consistently used.
     
    Stef_Morojna likes this.
  11. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Thanks for the help!
    Its all working!
     
    MD_Reptile likes this.