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 Vipirak · Feb 20, 2014 at 02:48 PM · c#addcomponent

Destroying Object when Adding Component

Hello

I am having a problem with, when i add a component (script) to an object, the whole object disappears completly, it have been tested and debugged over and over again, and it works all the time, except when the script is being added.

     public static void SpawnSpell(string SpellID, Vector3 CasterPos, Vector3 TargetPos)
     {
         GameObject spell = GameObject.CreatePrimitive(PrimitiveType.Cube);
         spell.transform.position = CasterPos;
         TravelSpell ts = spell.AddComponent<TravelSpell>();
         ts.destination = TargetPos;
         ts.spellID = SpellID;
         spell.AddComponent<Rigidbody>();
     }

The above is the method where the GameObject is apawned, and the Component added to it, it is somewhere at here or the below where the actual bug happens, there's no error message or anything. just no object either.

     public Vector3 destination
     {
         set
         {
             if (currentDestination != null)
             {
                 Destroy(currentDestination);
             }
             currentDestination = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             currentDestination.GetComponent<MeshRenderer>().enabled = false;
             currentDestination.transform.position = value;
             currentDestination.collider.isTrigger = true;
             transform.LookAt(currentDestination.transform);
             Movement = DistanceUnit(transform.position, value);
             currentDestination.AddComponent<Rigidbody>().useGravity = false;
         }
     }

note: the Sphere spawned (ln 9) spawns as it should

note: due to code being transferred from pictures to Code Samples, information about the examples may not be seen directly on the Code Samples, but can be seen on the attached pictures, please inform if you discover such place, so that it can be changed to match once again

[1]: /storage/temp/22471-udklip1.png

[2]: /storage/temp/22472-udklip2.png

udklip2.png (26.9 kB)
udklip1.png (8.7 kB)
Comment
Add comment · Show 5
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 robertbu · Feb 20, 2014 at 03:08 PM 1
Share

In the future, please past your code directly into the question and use the 101/010 button to format the code. Does the game still exist in the Hirerarcy? I assume 'SpellCore' is derived from '$$anonymous$$onobehaviour'?

avatar image vexe · Feb 21, 2014 at 12:06 PM 1
Share

Slick color theme I must say

avatar image Dblfstr · Feb 21, 2014 at 04:11 PM 0
Share

Are your caster and target positions correct? is it just spawning far away and you cant see it?

avatar image whydoidoit · Feb 21, 2014 at 04:42 PM 0
Share

What do Awake and Start do (if anything) in SpellCore?

avatar image Vipirak · Feb 21, 2014 at 10:54 PM 0
Share

i will in the future, as per request (starting now!), use the 101/010 button for the code, (sorry!), and SpellCore is derived from $$anonymous$$onobehaviour.

the caster position (during the test) is the players position as the player casted it, the target is taken raw if it is less than the spells range, and shortened if it's further away.

the "spell" (cube) disappears in the Editor Hierarchy.

Start can be seen below, the Xml document is loading correctly, (that have been a problem prior, but it's fixed and bugtested)

     private void Start()
     {
         if (!isInitiated)
         {
             isInitiated = true;
             SpellArtList = spellArtList;
             spellArtList = null;
 
             InitiateSpellSystem();
 
             Destroy(gameObject);
         }
     }

     private static void InitiateSpellSystem()
     {
         string spellID = null;
         TextAsset ta = (TextAsset)Resources.Load("XmlFiles/Spells");
         xDoc.LoadXml(ta.text);
         XmlNodeList XNL = xDoc.SelectSingleNode("Spells").ChildNodes;
         foreach (XmlNode XN in XNL)
         {
             spellID = XN.OuterXml;
             string[] spellss = spellID.Split('>');
             spellID = spellss[0];
             spellss = spellID.Split('<');
             spellID = spellss[1];
             sI.Add(spellID);
         }
     }

Sorry Vexe, no more color the$$anonymous$$g :/

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vipirak · Feb 22, 2014 at 05:15 AM

The answer... it wasn't spawned with trigger enabled... so obvious, yet so hidden

It's solved

Comment
Add comment · 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
0

Answer by whydoidoit · Feb 22, 2014 at 02:44 AM

So your base Start has a Destroy(gameObject) in it - which is, errm, destroying the game object!

Comment
Add comment · Show 8 · 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 Vipirak · Feb 22, 2014 at 04:08 AM 0
Share

yes, but 'isInitiated' is a static bool, and SpellCore is, at the beginning of the game placed on an empty gameObject in the scene.

so what is within that 'if', will only be called once in the entire game session.

avatar image whydoidoit · Feb 22, 2014 at 04:11 AM 0
Share

Spell core is the base class of all of your spells though right? So you should certainly be debug logging whether that code gets executed. It's going to be that or something similar in a part of your code you haven't shown us.

avatar image Vipirak · Feb 22, 2014 at 04:24 AM 0
Share

i just entered a little debugging here

     private void Start()
     {
         if (!isInitiated)
         {
             Debug.Log("hi! :D!");
             isInitiated = true;
             SpellArtList = spellArtList;
             spellArtList = null;
 
             InitiateSpellSystem();
 
             Destroy(gameObject);
         }
 
         spellsCasted++;
         Debug.Log(spellsCasted.ToString());
     }

alt text

note: You didnt point at anything is from another script, whenever the mouse hovers over nothing

also, here's all used variabled:

     private static List<object> SpellArtList = new List<object>();
     private static int spellsCasted = 0;
     private static bool isInitiated;
     public List<object> spellArtList = new List<object>();


spelldebug.png (47.7 kB)
avatar image whydoidoit · Feb 22, 2014 at 04:28 AM 0
Share

Something went wrong with the image post...

avatar image whydoidoit · Feb 22, 2014 at 04:45 AM 1
Share

Cool - so now I guess you need to look for anywhere else you Destroy an object (you are sure it's destroyed not just in totally the wrong place?) and put a log statement on each of those to identify what is causing it.

Show more comments

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

22 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 avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

AddComponent adds too many (C#) 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

C# Non-Static Member Rigidbody2D.MovePosition 1 Answer


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