I know it’s a noobie question and I searched the web for this, but I did not find something about a solution.
The old but gold thing: object reference, for example, I got this:
function example(item : GameObject){
for(var i : int = 0; i < size; i++){
if(items[i] == null){
items[i] = item;
break;
}
}
}
‘item’ will be destroyed after “example” has been called (with Destroy(…)). Therefore, ‘items*’ will be null, because the reference (‘item’) does not exist anymore.* Now i wanted to create something like: javascript* *var instance : GameObject = new GameObject();* * and assign the parameter to this object, but I have not found any solutions according to that. My only solution was to create a Instantiate, but I don’t want to create a new gameObject in the scene. I just have a wall in front of my eyes and the solution will be so simple, but I despair right now… So my question: how is it possible to assign function parameter to an object without any references (create a new object and assign it without references)
GameObjects always exist in the scene, with the single exception of prefabs. You can instantiate a prefab (or clone an existing object in the scene), or create one with new GameObject(), but either way, the result is a game object in the scene.
I don’t know what it means to “assign function parameter to an object.” And if what you want to do is create a new object and assign it somewhere, well, you can pass or assign new GameObject() directly, without needing a local variable, but why? What’s the harm in having an extra local variable or two, especially if it makes your code clearer?
So you are assigning the same reference to all the elements in the items array (when the element is currently null)? And when you Destroy the “item” object later on, all the references in items are null? Sorry but that is expected behavior with references. What do you want to do? Do a deep clone/copy of the orignal item? So that a new instance is created?
Gameobjects always exist in the scene, that was the keypoint. So I modified it with Tags and Instantiate Prefabs with the specific tag, if I need them.