Sorry for the long question, but I'm being specific in order to avoid answers I've already considered. This is the weirdest problem I've encountered in Unity thus far. What's happening is that the last instantiated GameObject of all GamObjects instantiated via code, is the only one that behaves ENTIRELY correctly in its attached InteractibleWorldObject script. I didn't realise they weren't behaving correctly until I added a feature that changes the reticule icon depending on the type of interaction selected for the GameObject with which the player is currently interacting.

Consider this code in the InteractibleWorldObject script:

Debug.Log("Interaction triggered");
this.imgReticule.sprite = Resources.Load<Sprite>("gui/img_interaction_icon/img_interaction_icon_"
        + this.interactionOptions[this.selectedInteractionOption].imageFileName) as Sprite;
Debug.Log("changed reticule");
this.playerControl.defaultReticule = false;

this.txtReticuleTitle.text = this.interactionOptions[this.selectedInteractionOption].prompt;
this.txtReticuleBody.text = this.objectName;
Debug.Log("updated reticule text");

THIS CODE IS ALL IN THE SAME BLOCK. THERE IS ALSO NOTHING WRONG WITH THIS CODE.

However, the last instantiated GameObject is the only one that ever executes the first few lines, which change the reticule icon. The reticule text changes as expected when interacting with ANY instantiated GameObject with the InteractibleWorldObject script attached. When I comment out the part that changes the reticule text, the text never appears when activating any of the instantiated GameObjects, so I know two things:

  1. It's nothing to do with Unity using a different version of the .cs file.
  2. This code block is not out of reach.

Originally I had a bunch of duplicate GameObjects in the scene but I learnt the hard way that it doesn't really work like that. Instead, because I still want to build the scene manually in the editor, I placed the objects as I wanted them and then re-instantiate them in code by looking for them and replacing them with prefabs using the properties of the manually placed transforms.

Here is the code that does that (I suspect it may be part of the problem):

Transform obj;
Vector3 position;
Quaternion rotation;
for (int i = 0; i < 10; i++)
{
    // get info from gameObject placed in scene manually
    obj = gameObject.transform.GetChild(i);
    Debug.Log(obj.name);
    position = obj.localPosition;
    rotation = obj.localRotation;

    if (this.prefabs.Contains(obj.gameObject.name))
    {
        obj.gameObject.SetActive(false);
        UnityEngine.Object.Instantiate(Resources.Load<GameObject>("prefabs/"
                + obj.gameObject.name) as GameObject, position, rotation, gameObject.transform);
        Destroy(obj.gameObject);
    }
}

This code also functions with the desired result, or at least I thought so until now. It still might not be a problem because everything else except those few lines executes just as expected.

What could possibly be the problem here? If any more code is needed to diagnose the problem I will provide it.

share|improve this question

OK, never mind. It wasn't what I thought it was! It was alternating between reticule icons every frame which has now been fixed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.