- Home /
[solved]how can i enable or disable a component of instantiated objects?
i've a couple of gameobjects to instantiate with dragmouse script.i instantiate a few gameobjects.then ive a toggle for enableing that dragmouse script of instantiated.but its not working properly.
Now my question is how can i disable or enable that script of instantiated with toggle.how can i use find()? how can i achieve it please help me out.
i got my solution finally. thanks guys for your support. i'm unable to post this as answer.tell me how to do this question as solved. here is the code.
GameObject array;
//trigger condition
array=GameObject.FindObjectsWithTag("Tag");
for(int i =0;i<array.Length;i++)
array[i].GetComponent<Drag$$anonymous$$ouse>().enabled=false;//or true.
//solved
Answer by rajavamsidhar_gvs · Aug 13, 2015 at 07:43 AM
Thanks everyone. Actually my need is i'll instantiate gameobjects which have dragmouse script but initially that script is disabled by manually.next ive a toggle for enable that script..i tried all what you have mentioned in answers.But here is i've a script for instantiating objects like cube,sphere. and in another script one toggle is there. if it is true script will enable and i'll change the position of particular gameobject with mouse.but it is working sometime but its not disabling all time. i added tag "Drag" for all my gameobjects which are going to instantiate.
so i want all gameobjects who have drag tag. then i'll use getcomponent().enabled=false/true. i tried with GameObject.FindWithTag. but i'm getting only first object. a want all in on array. Again Thanks for your effort.
Edit
i got my solution finally. thanks guys for your support. i'm unable to post this as answer.tell me how to do this question as solved. here is the code.
GameObject array;
//trigger condition array=GameObject.FindObjectsWithTag("Tag"); for(int i =0;i().enabled=false;//or true.
//solved
Ins$$anonymous$$d of GamObject.FindWithTag, do GameObject.FindObjectsWithTag(), and then cycle through that array :)
i'm not much good in coding.still i'm trying.but i'm not getting proper solution.
Show us the code you have now, it'll be easier to help you.
Answer by itsharshdeep · Aug 13, 2015 at 07:03 AM
Can you pls explain that How/Which gameObject's script you want to disable .. ( i.e. How you can decide that now this or that one gameObject should disable the 'dragmouse' script).
If you need the last one then the following code can work
GameObject currentLoadedModel;
currentLoadedModel = Instantiate (objTemp) as GameObject;
currentLoadedModel.GetComponent<dragmouse>().enabled = false;
i solved it. see my comment for my question.Thanks for support. :)
Answer by GiyomuGames · Aug 13, 2015 at 06:20 AM
I'm not sure I understand your question, but just in case:
GetComponent<YourScriptName>().enable = true/false
allows you to enable / disable a component.
Answer by Priyanshu · Aug 13, 2015 at 07:05 AM
Instantiate Object.
Get its component.
Disable it.
Instantiate( yourGameObject).GetComponent().enabled = false;
Answer by manel1 · Aug 13, 2015 at 09:09 AM
GameObject GameObjectName;
GameObjectName=GameObject.FindWithTag("TagName");
GameObjectName.GetComponent().enabled=false;