Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc · Jun 06, 2016 at 05:15 PM · c#instantiatespawn

Problem with Instantiated clones

Hi, I have a spawner with the following code: using UnityEngine; using System.Collections;

 public class Spawn : MonoBehaviour {
 
     public GameObject[] enemies;
     public int amount; 
     private Vector3 spawnPoint; 
 
 
     void Start() {
         amount = 2;
 
     }
 
     void Update () {
         enemies = GameObject.FindGameObjectsWithTag ("Enemy");
         amount = enemies.Length;
 
         if (amount < 100) {
             InvokeRepeating ("spawnEnemy",1,100f);
         }
     }
 
     void spawnEnemy() {
         spawnPoint.x = Random.Range (-20,20);
         spawnPoint.y = 0.5f;
         spawnPoint.z = Random.Range (-20,20);
 
 
 
         Instantiate (enemies[Random.Range(0, enemies.Length -1)], spawnPoint, Quaternion.identity);
     
         CancelInvoke();
     }
 
 
 
 }

I also have the code to make the enemies move, which looks like this:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EnemyMove : MonoBehaviour {
 
     public Transform track;
     private float moveSpeed = 3;
 
     public int score;
     public Text scoreText; 
     public int health;
     public Color damageColor = Color.red;
     Renderer rend;
 
 
 
 
 
     void Start() {
         PlayerPrefs.GetInt ("scorePref");
         score = PlayerPrefs.GetInt ("scorePref");
         health = 1000; 
         rend = GetComponent<Renderer> ();
         rend.material.color = Color.white;
     
 
     }
 
 
     void Update () {
         float move = moveSpeed * Time.deltaTime;
         transform.position = Vector3.MoveTowards (transform.position, track.position, move);
 
         PlayerPrefs.SetInt ("scorePref", score);
         if (scoreText.name == "Score") {
             scoreText.text = "Score: " + score;
         }
 
     
     
     }
     void OnTriggerEnter(Collider col) {
 
         if (col.gameObject.tag == "Bullet") {
             PlayerPrefs.SetInt ("scorePref", score);
 
             rend.material.color = damageColor;
 
             health -= 100;
             print ("I've lost health!");
 
             if (health <= 0) {
 
 
                 Destroy (GetComponent<MeshRenderer> ());
                 Destroy (GetComponent<BoxCollider> ());
                 Destroy (gameObject, 5);
 
             }
 
         
 
 
         
         
 
         }
         if (col.gameObject.tag == "Player") {
             Destroy (gameObject);
             Debug.Log ("Outch");
 
 
         }
     
 
 
 
     }
 
 
 
 }

When the object "Bullet" collides with the Enemy, the material changes to red. However, I sometimes get an error which says that some of my clones don't have a Renderer attached to them, even though the prefab "Enemy" which is being spawned does have a Renderer.

As well as this, does anyone know how to use PlayerPrefs to add to the score in the code?

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

1 Reply

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

Answer by RayCastX · Jun 06, 2016 at 07:01 PM

hmmm.. Maybe the bullets are colliding with other gameobjects without renderer. For example, terrain..

try to add this..

 if(rend != null) //do something

or

 if(this.GetComponent() != null)// do something.


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 UDN_2f0abe75-257a-49c7-aaed-cdb8b48980dc · Jun 07, 2016 at 02:31 PM 0
Share

@RayCastX There is nothing else it could have collided with though. Any ideas?

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

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a collision check when spawning with Instantiate 1 Answer

Instantiate spawn problem 1 Answer

Spawn a random object on more than one position 1 Answer

Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers

What is the most efficient way to spawn in a lot of cubes, like 10k? [c#] 2 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