Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 keshk · Dec 29, 2013 at 11:24 AM · c#destroyrespawn

Respawn target after destroying returns error

I am using unity3d c# to respawn an object after destroying it at a specific position. The following code returns an error message:

Quarternion to matrix conversion failed because input quaternion is invalid

I get multiple spawns of the prefab a couple of seconds followed by the error message and they spawn all over the screen.

My codes as follows. For the variable respawn, I dragged and dropped the prefab from Unity itself. Thanks for any guidance.

 Transform initialSpawn; 
     Transform respawn;
     public static bool dead = false;
 
     void Start()
     {
         initialSpawn = Instantiate(respawn, new Vector3(0,7,0), Quaternion.identity) as Transform;
         initialSpawn.parent = transform;
     }
 
 void Update () {
         if (dead == true)
         {
             //The line below is the one giving the error
             initialSpawn = Instantiate(respawn, new Vector3(4, 7, 0), Quaternion.identity) as Transform; 
 
 
             initialSpawn.parent = transform;
             StartCoroutine(pauseBeforeReSpawn(2));
             dead = false;
             Debug.Log("test working");
         }
 
  void OnTriggerEnter(Collider c)
     {
        if(c.gameObject.name =="barrel" || c.gameObject.name == "ground") 
        {
          Destroy(initialSpawn); 
          dead = true;
        }
     }
 
     IEnumerator pauseBeforeReSpawn(int seconds)  
     {
         yield return new WaitForSeconds(seconds);
     }
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
Best Answer

Answer by DiligentGear · Dec 29, 2013 at 12:06 PM

using SetActive() instead of Instantiate/Destroy.

 Public GameObject initialSpawn; // Changed
     Public GameObject respawn; // Changed
     public static bool dead = false;
      
     void Awake()
         {
             initialSpawn = Instantiate(respawn, new Vector3(0,7,0), Quaternion.identity) as GameObject; // Changed
             initialSpawn.transform.parent = transform;
         }
      
     void Update () {
             if (dead == true)
             {
                 StartCoroutine(pauseBeforeReSpawn(2f));
                 dead = false;
             }
      }
      
     void OnTriggerEnter(Collider c)
         {
            if(c.gameObject.name =="barrel" || c.gameObject.name == "ground") 
            {
              initialSpawn.SetActive(false); // Changed
              dead = true;
            }
         }
      
     IEnumerator pauseBeforeReSpawn(float seconds)  
         {
             yield return new WaitForSeconds(seconds);
             initialSpawn.transform.position = new Vector3(4, 7, 0);
             initialSpawn.SetActive(true); // Changed
             yield return null;
         }
 
Comment
Add comment · Show 4 · 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
avatar image keshk · Dec 29, 2013 at 07:26 PM 0
Share

I tried your code. In returns the same error message and multi spawns the object.

For the variable Transform respawn, I am making it public. I attach the above code to the object which I want to be respawned.

I also drag and drop the same object's prefab to the variable Transform respawn from unity Inspector. Am I doing anything wrong at these steps?

avatar image DiligentGear · Dec 29, 2013 at 08:19 PM 0
Share

I just edited the code. It spawns multiple objects because I was using Start rather than Awake. I'm sorry for the mistake.

What error did you get? If you got something like "Object reference not set to an instance of an object", try to drag and drop again. Now it uses GameObject ins$$anonymous$$d of Transform.

avatar image keshk · Dec 29, 2013 at 09:58 PM 0
Share

UnassignedReferenceException: The variable respawn of 'RandomSpawn' has not been assigned. You probably need to assign the respawn variable of the RandomSpawn script in the inspector.

NullReferenceException RandomSpawn.OnTriggerEnter (UnityEngine.Collider c) (at Assets/_Script/RandomSpawn.cs:25)

Hi edited to your latest version and got the above 2 errors.

I dragged and dropped the object again for respawn variable. I changed the initialSpawn to private since the value is being assigned in the codes. I double checked and the names of the objects are correct.

The object is created once, collides with the barrel and program crashes. Thanks for the time you taking to help. :)

avatar image DiligentGear · Dec 30, 2013 at 06:54 AM 0
Share

It's still complaining null reference. Program crashes since it doesn't know what "respawn" is. Did you make your respawn a Prefab?

You might want to try to add your "respawn" prefab into your scene, and add this line in Awake, before the instantiation:

 initialSpawn = GameObject.Find("respawn"); // note that it's finding object by its name. $$anonymous$$atch it if you have different name.

And you can always do Debug.log(initialSpawn) to check whether it's null or not.

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to respawn an enemy prefab after its destroyed 1 Answer

Renderer on object disabled after level reload 1 Answer

Issue accessing a GameObject through an Arraylist 2 Answers


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