x


Adds infinite # of components, using GameObject.AddComponent.

The title speeks for itself, i use GameObject.AddComponent to add a script to a object, but i adds infinite numbers of that script, and it starts lagging and the editor stops answering. Here's the code (AI.js):

var speed = 3.0;
var rotationSpeed = 5.0;
var shootRange = 15.0;
var attackRange = 30.0;
var shootAngle = 4.0;
var dontComeCloserRange = 5.0;
var delayShootTime = 0.35;
var pickNextWaypointDistance = 2.0;
var target : Transform;
var objektnamn : GameObject;
private var lastShot = -10.0;

// Make sure there is always a character controller
@script RequireComponent (CharacterController)

function Update ()
{
   if ( Input.GetMouseButtonDown(1) )
   {
      var gunnar : RaycastHit;
      var ivar : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      if (Physics.Raycast (ivar, gunnar, 1000.0))
      {
        Debug.Log(gunnar.collider.gameObject.name);
        objektnamn = gunnar.collider.gameObject;
      }
   }

   if (this.gameObject.name != objektnamn.name) 
   {
     gameObject.AddComponent (AI);
   }
   else if (this.gameObject.name==objektnamn.name)
   {
    //GetComponent(AI).enabled = false; 
     Destroy (GetComponent (AI));
   } 
}

It's supposed to deactivate the AI script, when the object the script is attached to, is selected (Using raycast), the objektnamn is the variable containg the name of the currently selected object. Then, when a other object is selected, i want the AI script to activate again. Whats the problem here guys? Thanks in advance!

more ▼

asked Mar 15 '11 at 08:03 PM

Tommy gravatar image

Tommy
156 20 22 27

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

4 answers: sort voted first

That looks a bit weird. Where do you execute this code? in Awake, Start or Update? And in what script do you use this code? The AI script itself? What is "objektnamn" a string? or an object reference?

And finally the most important question: What do you want to achieve?
To me it looks like you do that in the AI script itself but that means that it have to be attached to an object and it adds itself to the object. The new instance you've just added will do the same and add another instance...

If you have new information edit your question and don't post an answer if it's not an answer to the question. You should at least include the function that contains this code snippet and what's the type of your "objektnamn" variable.


edit
Ok, I still can't figure out what's your setup in your scene. You just talk about objects that should be selected. Do they have also an AI script?

Anyways, I guess that will never come to an end. Just some hints:

  • You execute either Destroy(GetComponent(AI)) or AddComponent(AI) every frame because you used it in Update(). That means it would add infinite AI Components or will throw endless errors after the last one is deleted.
  • The AI script add itself to the gameobject so every new added instance will do the same.
  • The Raycast selection code should be there only once. Normally such code is attached to the camera. In your case if you have 3 objects in the scene with the AI script attached all 3 will execute the raycast.
  • Your condition else if(this.gameObject.name == objektnamn.name) is useless since it's just the opposite of (this.gameObject.name != objektnamn.name). The else would be enough.
  • If you want to activate/deactivate a script you don't need to destroy the script. You just can deactivate it via enable.

Here's an example script that should be attached to the camera which do the selection stuff.
(Note: that's a seperate script. This script can only select objects that have an AI script attached)

var curentSelection : AI = null;

function Update ()
{
    if ( Input.GetMouseButtonDown(1) )
    {
        var hit : RaycastHit;
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        if (Physics.Raycast (ray, hit, 1000.0))
        {
            // Try to get the AI script of the new selection
            var tempAI : AI = hit.collider.gameObject.GetComponent.<AI>();

            // If we selected a new one, deactivate the script.
            if (tempAI != null)
            {
                // first reactivate the last selected object if there is one
                if (curentSelection != null)
                    curentSelection.enabled = true;

                // make the new selected one the current selected.
                curentSelection = tempAI;
                // deactivate the script
                curentSelection.enabled = false;
            }
        }
    }
}
more ▼

answered Mar 15 '11 at 10:49 PM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

Iv'e edited the question.

Mar 15 '11 at 11:02 PM Tommy

You didn't tell what's the scripts name. Is that script above the AI script? Also there are some things that can't really work. You placed your AddComponent part in the script body. You really should use a designated function like Start() for such things. The code in the body get executed after OnEnable() but you set your objektnamn variable in Update so it won't be set when your code is executed. And you still haven't explained what you want to do.

Mar 15 '11 at 11:26 PM Bunny83

Edited again...

Mar 16 '11 at 01:48 PM Tommy

The script works awesome, thank you! But still, the objects won't stop walking around (The AI scripts makes em' follow waypoints) when the AI script isn't enabled. That's why i used Destroy().

Mar 16 '11 at 02:53 PM Tommy
(comments are locked)
10|3000 characters needed characters left
if (this.gameObject.name!=objektnamn) 
{
 if(!objektnamn.AI)
 {
  gameObject.AddComponent (AI);
 }
}
more ▼

answered Mar 16 '11 at 07:55 PM

zmar0519 gravatar image

zmar0519
946 59 66 78

Still get error: BCE0019: 'AI' is not a member of 'UnityEngine.GameObject'. on the line: if(!objektnamn.AI).

Mar 17 '11 at 01:10 PM Tommy

I'm not quite sure why it is not working. try just AddComponent, not gameObject.AddComponent.

Mar 17 '11 at 07:06 PM zmar0519

That won't solve the problem on line: if(!objektnamn.AI) unfortunately.

Mar 17 '11 at 08:36 PM Tommy
(comments are locked)
10|3000 characters needed characters left

your code is fine, but you are just missing an if statement. try this:

   if (this.gameObject.name==objektnamn)
   {
    //GetComponent(AI).enabled = false; 
     Destroy (GetComponent (AI));
   } 
   else if (this.gameObject.name!=objektnamn) 
   {
    if(!objektnamn.AI)
    {
         gameObject.AddComponent (AI);
    }
   }
more ▼

answered Mar 15 '11 at 08:23 PM

zmar0519 gravatar image

zmar0519
946 59 66 78

It stills adds infinite number of the AI script :/

Mar 15 '11 at 08:31 PM Tommy

Also, "objektnamn" is string, so i get error "Assets/Scripts/AI.js(37,20): BCE0019: 'AI' is not a member of 'String'.", on the line "if(!objektnamn.AI)".

Mar 15 '11 at 08:33 PM Tommy

The reason that it does not work is because objektnamn is a string. Change it to a GameObject, and change else to else is(this.gameObject.name != objektnamn.name)

Mar 15 '11 at 08:43 PM zmar0519

Sorry about the spelling error, else if(this.gameObject.name != objektnamn.name)

Mar 15 '11 at 08:44 PM zmar0519

Thank you Joe, it's improving, almost there now i belive. Now the only problem is that, as soon as i run the game, the AI script get's destroyd. It's probably something really simple that iv'e should have seen. Here's the code:

if (this.gameObject.name==objektnamn) { //GetComponent(AI).enabled = false; Destroy (GetComponent (AI)); } else if(this.gameObject.name != objektnamn.name) { gameObject.AddComponent (AI); }

Mar 15 '11 at 08:50 PM Tommy
(comments are locked)
10|3000 characters needed characters left

The code:

    if (this.gameObject.name==objektnamn)
   {
    //GetComponent(AI).enabled = false; 
     Destroy (GetComponent (AI));
   } 
   else if(this.gameObject.name != objektnamn.name) 
   {
     gameObject.AddComponent (AI);
   }
more ▼

answered Mar 15 '11 at 08:50 PM

Tommy gravatar image

Tommy
156 20 22 27

Is that an answer to your question? If not you should edit your question and don't post comments as answers. Please read the FAQs. http://answers.unity3d.com/faq

Mar 15 '11 at 10:56 PM Bunny83
(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:

x2177
x314
x89
x35

asked: Mar 15 '11 at 08:03 PM

Seen: 1238 times

Last Updated: Mar 16 '11 at 01:49 PM