Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10551

Assigned single Object to multiple script - How many instances

I Have two classes clsA, clsB (both are non-static). clsB is attached to 20 GameObject while clsA has attached to single GameObject only. Now, clsB is using ClsA as a refrence. I made a public Variable of clsA gameObject in clsB and now accessing its property in clsB which is attaced 20 Gameobjects(means 20 scripts to 20 gameoBjects).

My simple Question weather all 20 contains a single clsA object or all contain (ClsA) different objects/instances.

I know the difference between value type and refrence type but I am confuse at this point cause I am facing some issue then I ask this question well my own understanding said that all 20 scripts(clsB) have only single instance of clsB. Please clarify ASAP.

As Answer Suggested I wtie this code to get the instance Id. surprisingly it is showing same Instance Id.

avgManager = GameObject.Find("AGV Manager").GetComponent<AVGManager>();
        Debug.Log(gameObject.name  + " : " + avgManager.GetInstanceID());

Upvotes: 0

Views: 246

Answers (2)

Muhammad Faizan Khan
Muhammad Faizan Khan

Reputation: 10551

My simple Answer is all 20 contains a single clsA object their is only one instance made and assigned to all 20 objects as I have check by instance id all showing same Id(means instance number).

Debug.Log(gameObject.name  + " : " + avgManager.GetInstanceID());

avgManager is assigned to 20 places and all 20 place showing me the same instance ID. GetInstanceID Returns the instance id of the object.

Upvotes: 0

Carbine
Carbine

Reputation: 7903

Every clsB will have its own instance of clsA. As there are 20 ClsB there will be 20 clsA object. You can verify this by doing - Make Object ID in the visual studio debugger.

Add the variable to the watch window, rightclick on the variable and select MakeObjectId. Do this again for a different instance of clsB and clsA inside it. If they dont match the same id, you can be sure they are not of the same instance.

Upvotes: 1

Related Questions