Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tommy · Mar 15, 2011 at 08:03 PM · gameobjectcomponentaddinfinite

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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). 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;
         }
     }
 }

}

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tommy · Mar 15, 2011 at 11:02 PM 0
Share

Iv'e edited the question.

avatar image Bunny83 · Mar 15, 2011 at 11:26 PM 0
Share

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.

avatar image Tommy · Mar 16, 2011 at 01:48 PM 0
Share

Edited again...

avatar image Tommy · Mar 16, 2011 at 02:53 PM 0
Share

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().

avatar image
0

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);
    }
   }
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tommy · Mar 15, 2011 at 08:31 PM 0
Share

It stills adds infinite number of the AI script :/

avatar image Tommy · Mar 15, 2011 at 08:33 PM 0
Share

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)".

avatar image zmar0519 · Mar 15, 2011 at 08:43 PM 0
Share

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)

avatar image zmar0519 · Mar 15, 2011 at 08:44 PM 0
Share

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

avatar image Tommy · Mar 15, 2011 at 08:50 PM 0
Share

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); }

Show more comments
avatar image
0

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);
   }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 15, 2011 at 10:56 PM 0
Share

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

avatar image
0

Answer by zmar0519 · Mar 16, 2011 at 07:55 PM

if (this.gameObject.name!=objektnamn) 
{
 if(!objektnamn.AI)
 {
  gameObject.AddComponent (AI);
 }
}
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tommy · Mar 17, 2011 at 01:10 PM 0
Share

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

avatar image zmar0519 · Mar 17, 2011 at 07:06 PM 0
Share

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

avatar image Tommy · Mar 17, 2011 at 08:36 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges