- Home /
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!
Answer by Bunny83 · Mar 15, 2011 at 10:49 PM
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)
. Theelse
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;
}
}
}
}
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.
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().
Answer by zmar0519 · Mar 15, 2011 at 08:23 PM
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);
}
}
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)".
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)
Sorry about the spelling error, else if(this.gameObject.name != objektnamn.name)
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); }
Answer by Tommy · Mar 15, 2011 at 08:50 PM
The code:
if (this.gameObject.name==objektnamn)
{
//GetComponent(AI).enabled = false;
Destroy (GetComponent (AI));
}
else if(this.gameObject.name != objektnamn.name)
{
gameObject.AddComponent (AI);
}
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
Answer by zmar0519 · Mar 16, 2011 at 07:55 PM
if (this.gameObject.name!=objektnamn)
{
if(!objektnamn.AI)
{
gameObject.AddComponent (AI);
}
}
Still get error: BCE0019: 'AI' is not a member of 'UnityEngine.GameObject'. on the line: if(!objektnamn.AI).
I'm not quite sure why it is not working. try just AddComponent, not gameObject.AddComponent.
That won't solve the problem on line: if(!objektnamn.AI) unfortunately.
Your answer
Follow this Question
Related Questions
Get GameObject from List based on attached script or assigned name 3 Answers
How do I extract all the components of a gameobject and place them in to a new gameobject? 1 Answer
GameObject with DirectionalLight component not being iluminated 1 Answer
How to Change Background Sprite at run time ? 0 Answers
How to set GameObject's component to an instance of an object 0 Answers