Subversion Repository Public Repository

Nextrek

Diff Revisions 167 vs 168 for /3DSpace/Assets/SpaceUnity/Scripts/SU_AsteroidField.cs

Diff revisions: vs.
  @@ -211,22 +211,22 @@
211 211 // ...and if the bool flag has been not been set...
212 212 if (!_asteroidsFading[i]) {
213 213 // Change the material of the asteroid to a transparent material that supports alpha fading
214 - _asteroid.renderer.sharedMaterial = (Material) _materialsTransparent[_asteroidsMaterials[i]];
214 + _asteroid.GetComponent<Renderer>().sharedMaterial = (Material) _materialsTransparent[_asteroidsMaterials[i]];
215 215 // Set the fading flag to true (we compare a Bool list instead of costly checking the material in each frame
216 216 _asteroidsFading[i] = true;
217 217 }
218 218 // Cache the color of the material
219 219
220 - Color _col = _asteroid.renderer.material.color;
220 + Color _col = _asteroid.GetComponent<Renderer>().material.color;
221 221 // Calculate the new alpha value between _distanceToFade (1.0) and _distanceToSpawn (0.0)
222 222 float _alpha = Mathf.Clamp01(1.0f - ((_distance - _distanceToFade) / (_distanceToSpawn - _distanceToFade)));
223 223 // Set the same color with the new alpha value to the asteroid
224 - _asteroid.renderer.material.color = new Color(_col.r, _col.g, _col.b, _alpha );
224 + _asteroid.GetComponent<Renderer>().material.color = new Color(_col.r, _col.g, _col.b, _alpha );
225 225 } else {
226 226 // The asteroid is within range, and if the fading flag is still set...
227 227 if (_asteroidsFading[i]) {
228 228 // Change the material back to the original non-transparent material
229 - _asteroid.renderer.material = (Material) _asteroidsMaterials[i];
229 + _asteroid.GetComponent<Renderer>().material = (Material) _asteroidsMaterials[i];
230 230 // Set the fading falg to false to prevent material changes each frame
231 231 _asteroidsFading[i] = false;
232 232 }
  @@ -278,23 +278,23 @@
278 278 // Set a random material of the asteroid based on the weighted probabilty list
279 279 switch (WeightedRandom(_materialList)) {
280 280 case "VeryCommon":
281 - _newAsteroid.renderer.sharedMaterial = materialVeryCommon[Random.Range(0, materialVeryCommon.Length)];
281 + _newAsteroid.GetComponent<Renderer>().sharedMaterial = materialVeryCommon[Random.Range(0, materialVeryCommon.Length)];
282 282 break;
283 283 case "Common":
284 - _newAsteroid.renderer.sharedMaterial = materialCommon[Random.Range(0, materialCommon.Length)];
284 + _newAsteroid.GetComponent<Renderer>().sharedMaterial = materialCommon[Random.Range(0, materialCommon.Length)];
285 285 break;
286 286 case "Rare":
287 - _newAsteroid.renderer.sharedMaterial= materialRare[Random.Range(0, materialRare.Length)];
287 + _newAsteroid.GetComponent<Renderer>().sharedMaterial= materialRare[Random.Range(0, materialRare.Length)];
288 288 break;
289 289 case "VeryRare":
290 - _newAsteroid.renderer.sharedMaterial = materialVeryRare[Random.Range(0, materialVeryRare.Length)];
290 + _newAsteroid.GetComponent<Renderer>().sharedMaterial = materialVeryRare[Random.Range(0, materialVeryRare.Length)];
291 291 break;
292 292 }
293 293
294 294 // Add the asteroid to a list used to keep track of them
295 295 _asteroids.Add(_newAsteroid);
296 296 // A list to store which material each asteroid has
297 - _asteroidsMaterials.Add(_newAsteroid.renderer.sharedMaterial);
297 + _asteroidsMaterials.Add(_newAsteroid.GetComponent<Renderer>().sharedMaterial);
298 298 // Improve performance by having a list of bool values whether or not asteroids are fading
299 299 _asteroidsFading.Add(false);
300 300
  @@ -303,7 +303,7 @@
303 303 // Set the mesh of the asteroid based on chosen polycount
304 304 _newAsteroid.GetComponent<SU_Asteroid>().SetPolyCount(polyCount);
305 305 // If the asteroid has a collider...
306 - if (_newAsteroid.collider != null) {
306 + if (_newAsteroid.GetComponent<Collider>() != null) {
307 307 _newAsteroid.GetComponent<SU_Asteroid>().SetPolyCount(polyCountCollider, true);
308 308 }
309 309 }
  @@ -318,13 +318,13 @@
318 318 if (isRigidbody) {
319 319 // RIGIDBODY ASTEROIDS
320 320 // If the asteroid prefab has a rigidbody...
321 - if (_newAsteroid.rigidbody != null) {
321 + if (_newAsteroid.GetComponent<Rigidbody>() != null) {
322 322 // Set the mass to mass specified in AsteroidField mutiplied by scale
323 - _newAsteroid.rigidbody.mass = mass * _newScale;
323 + _newAsteroid.GetComponent<Rigidbody>().mass = mass * _newScale;
324 324 // Set the velocity (speed) of the rigidbody to within the min/max velocity range multiplier by velocityMultiplier
325 - _newAsteroid.rigidbody.velocity = _newAsteroid.transform.forward * Random.Range(minAsteroidVelocity, maxAsteroidVelocity) * velocityMultiplier;
325 + _newAsteroid.GetComponent<Rigidbody>().velocity = _newAsteroid.transform.forward * Random.Range(minAsteroidVelocity, maxAsteroidVelocity) * velocityMultiplier;
326 326 // Set the angular velocity (rotational speed) of the rigidbody to within the min/max velocity range multiplier by velocityMultiplier
327 - _newAsteroid.rigidbody.angularVelocity = new Vector3(Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f)) * Random.Range(minAsteroidAngularVelocity, maxAsteroidAngularVelocity) * angularVelocityMultiplier;
327 + _newAsteroid.GetComponent<Rigidbody>().angularVelocity = new Vector3(Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f), Random.Range(0.0f,1.0f)) * Random.Range(minAsteroidAngularVelocity, maxAsteroidAngularVelocity) * angularVelocityMultiplier;
328 328 } else {
329 329 Debug.LogWarning("AsteroidField is set to spawn rigidbody asterodids but one or more asteroid prefabs do not have rigidbody component attached.");
330 330 }
  @@ -332,9 +332,9 @@
332 332 // NON-RIGIDBODY ASTEROIDS
333 333
334 334 // If the asteroid prefab has a rigidbody...
335 - if (_newAsteroid.rigidbody != null) {
335 + if (_newAsteroid.GetComponent<Rigidbody>() != null) {
336 336 // Destroy the rigidbody since the asteroid field is spawning non-rigidbody asteroids
337 - Destroy(_newAsteroid.rigidbody);
337 + Destroy(_newAsteroid.GetComponent<Rigidbody>());
338 338 }
339 339 // If the asteroid has the Asteroid script attached to it...
340 340 if (_newAsteroid.GetComponent<SU_Asteroid>() != null) {