x


enable / disable a script without dynamic typing

I'm trying to move my project over to my mac laptop and unity iOS and have unearthed a number of issues due to dynamic typing. I have gotten rid of most but am left with a few outstanding issues this being one of them.

At one point I am trying to reference a script to disable it so I don't get any interference from it during another part of the game. Here is a code snip.

  if (GUI.Button (position5, plankPhy, style5))
        if (Physics.Raycast (ray, hit, 300)) {
            Instantiate(PhyPlank, hit.point, Quaternion.identity);
            GetComponentInChildren(ClickSpawn).enabled = false;
            Time.timeScale = .0000001;

This works great when playing for PC or web but as soon as I put it on my iOS build (or in this case throw "#pragma strict" at the top of my code for working on my pc I get an error...

BCE0019: 'enabled' is not a member of 'UnityEngine.Component'.

What's happening in the script is when an object the player moves and places down in the map is spawned it freezes time to allow the player to place the object but it also disables a script that generates geo when the mouse button is clicked.

There is a second part to the script that references the script again to enable it again. Any help as always is very much appreciated. Thank you!

more ▼

asked Apr 16 '11 at 08:10 PM

Earth-O-Matic gravatar image

Earth-O-Matic
70 9 9 18

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Alright figured it out!

So to get the code to disable it without dynamic typing (needed for iOS) It needs to be written long hand like this:

GameObject.Find("TheGameObjectWithYourScript");
(GetComponent(YourScript) as YourScript).enabled = false;

once it's been disabled. on the next set of script to enable it again (I'm assuming since it has been called - I'm new at this so shrug) you define the game object in a var and in my case using mouse up it would look something like this:

var clickItem : GameObject;

function OnMouseUp(){

     (clickItem.GetComponent(YourScript) as YourScript).enabled = true;

}

So this is how it works for me to call a script to enable and disable it without dynamic typing.

more ▼

answered Apr 16 '11 at 09:43 PM

Earth-O-Matic gravatar image

Earth-O-Matic
70 9 9 18

By the way, the enable and disable are on two separate scrips for my project which is why I need to define twice.

Apr 16 '11 at 09:44 PM Earth-O-Matic

So you had to Find()... as I suggest you...

Apr 16 '11 at 09:48 PM Uzquiano

roll eyes... remove the GameObject.Find line.... it has no use in your script. The semicolon terminates the statement. Your GameObject.Find line doesn't belong to the second line in any way...

Apr 16 '11 at 10:43 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

You have to cast the Component reference you get back from GetComponent into the right type, or just use the generic function.

GetComponentInChildren.<ClickSpawn>().enabled = false;

or the old casting way:

(GetComponentInChildren(ClickSpawn) as ClickSpawn).enabled = false;
more ▼

answered Apr 16 '11 at 09:41 PM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

Why was this downvoted? It's correct.

Apr 16 '11 at 11:35 PM Eric5h5

I guess Uzquiano don't like me :D but not sure ...

Apr 16 '11 at 11:39 PM Bunny83

??? Anyway, if you mean for the discussion below, I am newer than you in using Unity but I think instead of saying always that using Find() is kind of sin, it must be said that that Find() must be used only inside Awake() and Start(). I think that there are more memory inefficient behaiviours that people care less and affect more.

Apr 17 '11 at 03:19 PM Uzquiano
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x231
x215
x133
x132
x111

asked: Apr 16 '11 at 08:10 PM

Seen: 1258 times

Last Updated: Apr 16 '11 at 08:10 PM