- Home /
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
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'?
Are your caster and target positions correct? is it just spawning far away and you cant see it?
What do Awake and Start do (if anything) in SpellCore?
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 :/
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
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!
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.
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.
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());
}
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>();
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.
Your answer
Follow this Question
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