Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Code not working correctly.

Discussion in 'Scripting' started by jesters_specter, Oct 1, 2014.

  1. jesters_specter

    jesters_specter

    Joined:
    Aug 27, 2014
    Posts:
    7
    I'm making an AI for the enemy in my game, I'm trying to make the clones not clone themselves, But they still do.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ENAI : MonoBehaviour {
    5.    
    6.     // Use this for initialization
    7.     public bool canSpawn = true;
    8.     // Update is called once per frame
    9.     void Update() {
    10.         if(canSpawn == true){
    11.             int rndizer = Random.Range(1, 51);
    12.             if (rndizer == 1 ) {
    13.                 Rigidbody clone;
    14.                 clone = Instantiate (GameObject.Find("Enemy"), transform.position, transform.rotation) as Rigidbody;
    15.                 clone.AddForce (Vector3.forward / 40);
    16.                 if (gameObject.transform.name == "Enemy(Clone)") {
    17.                 GetComponent<ENAI>().canSpawn = false;
    18.                 }
    19.             }
    20.         }
    21.     }
    22. }
     
  2. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    You need to do clone.GetComponent<ENAI>().... etc.
     
  3. jesters_specter

    jesters_specter

    Joined:
    Aug 27, 2014
    Posts:
    7
    This did nothing to solve the issue.
     
  4. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Then you'll have to explain the issue with a little more detail. t seems to me you are using this script to spawn other objects which also contain this script, but you're setting canSpawn to false so the copies cannot create more.

    Why not disable/remove the script component in the copies?
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Use prefabs for the instantiate commands.
    If the instantiated clones should never be able to spawn clones themselves, you can create the prefab without attaching the script.
     
  6. jesters_specter

    jesters_specter

    Joined:
    Aug 27, 2014
    Posts:
    7
    I don't know how to do that, I'm new to unity.
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's pretty simple and highly recommended. Then you'll get to know it now. ;)

    Import or create your object in your scene. If neccessary, add components (in your case a Rigidbody) and other stuff such as scripts etc (leave it for this example). When you're done, you can simply drag and drop it into the project window. You'll see the name in the hierarchy turns blue. Of course you can still add/remove or completely change the prefab later on.

    As for your script:
    First, add a public variable to your script (in this case of type Rigidbody).

    Code (CSharp):
    1. public Rigidbody prefabToSpawn;
    If you save the script and go back into Unity, you'll see a new slot for an object with 'None (Rigidbody)'. Drag and drop your prefab from the project window (i recommend to not take it from the hierarchy window) onto this slot.
    When you've done everything correctly, you'll see '<nameOfYourPrefab> (Rigidbody)', if you cannot drag and drop it, re-read the steps above.

    Now, as you don't want the 'clones' to spawn clones themselves, adjust your piece of code.
    In the instatiate method, replace the first parameter with the variables name, i called it 'prefabToSpawn' in the declaration above.
    Last but not least, remove the third if-statement and the next line from your script (line 16 and 17 in your code above).

    Test it, if you cannot see a force being applied to it, change the value in the AddForce(...) method (although they should already react to each others colliders).

    Errors will be shown in the console, always have a look at it when testing your game.
     
  8. jesters_specter

    jesters_specter

    Joined:
    Aug 27, 2014
    Posts:
    7
    Now they're not responding to clone.addForce();
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    How much force do you apply to them?
    The force in your first script (1/40 on one axis) is not very much. Have you tried to increase it?
     
  10. jesters_specter

    jesters_specter

    Joined:
    Aug 27, 2014
    Posts:
    7
    Yes, I even tried one hundred million.
    EDIT: The game thinks i'm trying to make the spawn point move, Not the clone.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ENAI : MonoBehaviour {
    5.    
    6.     // Use this for initialization
    7.     public Rigidbody pref2spawn;
    8.     // Update is called once per frame
    9.     void Update() {
    10.             int rndizer = Random.Range(1, 51);
    11.             if (rndizer == 1 ) {
    12.                 Rigidbody clone;
    13.                 clone = Instantiate (pref2spawn, transform.position, transform.rotation) as Rigidbody;
    14.                 clone.AddForce(Vector3.forward * 1000000000);
    15.         }
    16.     }
    17. }
     
    Last edited: Oct 11, 2014
  11. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,605
    Why are you assigning a Prefab to a variable of type Rigidbody? GameObjects are what you instantiate, so you want your variable to be type GameObject.

    This should work. Just make sure you assign the Prefab you want to instantiate to the pref2span field in the inspector. A prefab must be dragged from the Project window to the slot in the inspector.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ENAI : MonoBehaviour {
    5.       public GameObject pref2spawn; // <-- Assign a Prefab to this slot
    6.  
    7.       void Update() {
    8.           int rndizer = Random.Range(1, 51);
    9.           if (rndizer == 1 ) {
    10.               GameObject clone = (GameObject)Instantiate(pref2spawn, transform.position, transform.rotation);
    11.               Rigidbody rb = clone.GetComponent<Rigidbody>();
    12.               if(rb != null) {
    13.                   rb.AddForce(Vector3.forward * 10, ForceMode.Impulse);
    14.               }
    15.         }
    16.   }
    17. }
    Additionally, on the force, if you're applying the force during only one frame, you probably want to use ForceMode.Impulse to make it an instant force. If you apply a non-impulse force only on the first frame the object is instantiated, it will probably not move much without a huge force.
     
    Last edited: Oct 11, 2014
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Good point with the ForceMode, but the rigidbody can be used as an object to be spawned. There's nothing wrong with it as he returns the rigidbody component with 'as Rigidbody'.