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 /
avatar image
-1
Question by Chocolade · Oct 17, 2016 at 06:41 PM · c#scripting problemscript.scripting beginnerscriptingbasics

Why when using get; set; in one script i'm getting null in other script that use it ?

This is the first script that clone objects: It does clone the objects but this script and the second one both attached to the same gameobject but for some reason the second script is running first so when i call the public GameObject[] Spaceships get; set; it's null the Spaceships return null. And the script CloneSpaceShips should be running first. Why the editor decide to run first the second script the ObjectsRandomMovements script ?

 using System;
 using UnityEngine;
 using Random = UnityEngine.Random;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class CloneSpaceShips : MonoBehaviour {
 
     public GameObject Spaceship;
     private GameObject[] spaceships;
 
     // for tracking properties change
     private Vector3 _extents;
     private int _spaceshipCount;
     private float _spaceshipSize;
 
     /// <summary>
     ///     How far to place spheres randomly.
     /// </summary>
     public Vector3 Extents;
 
     /// <summary>
     ///     How many spheres wanted.
     /// </summary>
     public int SpaceShipCount;
     public float SpaceShipSize;
 
     // Use this for initialization
     void Start () {
 
         Clone();
         spaceships = GameObject.FindGameObjectsWithTag("SpaceShipToClone");
 
     }
 
     public GameObject[] Spaceships
     {
         get
         {
             return spaceships;
         }
         set
         {
             spaceships = value;
         }
     }
 
     private void OnValidate()
     {
         // prevent wrong values to be entered
         Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
         SpaceShipCount = Mathf.Max(0, SpaceShipCount);
         SpaceShipSize = Mathf.Max(0.0f, SpaceShipSize);
     }
 
     private void Reset()
     {
         Extents = new Vector3(250.0f, 20.0f, 250.0f);
         SpaceShipCount = 100;
         SpaceShipSize = 20.0f;
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 
     private void Clone()
     {
         if (Extents == _extents && SpaceShipCount == _spaceshipCount && Mathf.Approximately(SpaceShipSize, _spaceshipSize))
             return;
 
         // cleanup
         var ShipsToDestroy = GameObject.FindGameObjectsWithTag("SpaceShip");
         foreach (var t in ShipsToDestroy)
         {
             if (Application.isEditor)
             {
                 DestroyImmediate(t);
             }
             else
             {
                 Destroy(t);
             }
         }
 
         var withTag = GameObject.FindWithTag("Terrain");
         if (withTag == null)
             throw new InvalidOperationException("Terrain not found");
 
         for (var i = 0; i < SpaceShipCount; i++)
         {
             var o = Instantiate(Spaceship);
             o.tag = "SpaceShip";
             o.transform.SetParent(base.gameObject.transform);
             o.transform.localScale = new Vector3(SpaceShipSize, SpaceShipSize, SpaceShipSize);
 
             // get random position
             var x = Random.Range(-Extents.x, Extents.x);
             var y = Extents.y; // sphere altitude relative to terrain below
             var z = Random.Range(-Extents.z, Extents.z);
 
             // now send a ray down terrain to adjust Y according terrain below
             var height = 10000.0f; // should be higher than highest terrain altitude
             var origin = new Vector3(x, height, z);
             var ray = new Ray(origin, Vector3.down);
             RaycastHit hit;
             var maxDistance = 20000.0f;
             var nameToLayer = LayerMask.NameToLayer("Terrain");
             var layerMask = 1 << nameToLayer;
             if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
             {
                 var distance = hit.distance;
                 y = height - distance + y; // adjust
             }
             else
             {
                 Debug.LogWarning("Terrain not hit, using default height !");
             }
 
             // place !
             o.transform.position = new Vector3(x, y, z);
         }
 
         _extents = Extents;
         _spaceshipCount = SpaceShipCount;
         _spaceshipSize = SpaceShipSize;
     }
 }
 

This is the second script but it's running before the CloneSpaceShips so when it's doing this part in the Start function:

 CloneSpaceShips ss = new CloneSpaceShips ();
 ObjectsToMove = ss.Spaceships;

Then the ObjectsToMove is null since it didn't clone yet the objects in the first script.

What am i missing here ?

 using System;
 using UnityEngine;
 using Random = UnityEngine.Random;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class ObjectsRandomMovements : MonoBehaviour {
 
     private GameObject[] ObjectsToMove;
     public float moveSpeed = 50;
 
     private float distanceTravelled;
     public bool updateOn = true;
     private Vector3 lastPosition;
 
     List<bool> hasRotated = new List<bool>();
     List<float> distanceTraveled = new List<float>();
     List<Vector3> lastPositions = new List<Vector3>();
     List<int> randomNumbers = new List<int>();
     private bool test = false;
 
     public float smooth = 1f;
     private Vector3 targetAngles;
 
     private void rndNumbers()
     {
         int Min = 5;
         int Max = 500;
         System.Random rnd = new System.Random();
         randomNumbers = Enumerable.Range(Min, Max).OrderBy(x => rnd.Next()).Take(ObjectsToMove[0].transform.childCount).ToList();
     }
 
     // Use this for initialization
     void Start () {
 
             CloneSpaceShips ss = new CloneSpaceShips ();
             ObjectsToMove = ss.Spaceships;
 
         rndNumbers();
 
         for (int index = 0; index < ObjectsToMove[0].transform.childCount; index++)
         {
             Transform child = ObjectsToMove[0].transform.GetChild(index);
             lastPosition = new Vector3(child.transform.position.x, child.transform.position.y, child.transform.position.z);
             lastPositions.Add(lastPosition);
             hasRotated.Add(false);
             distanceTraveled.Add(0f);
         }
     }
     
     // Update is called once per frame
     void Update () {
 
         MoveObjects();
     }
 
     private void MoveObjects()
     {
         for (int index = 0; index < ObjectsToMove[0].transform.childCount; index++)
         {
             Transform oneChild = ObjectsToMove[0].transform.GetChild(index);
             lastPositions[index] = oneChild.transform.position;
             oneChild.transform.position += oneChild.transform.forward * Time.deltaTime * moveSpeed;
         }
 
         if (updateOn == true)
         {
             for (int index = 0; index < ObjectsToMove[0].transform.childCount; index++)
             {
                 Transform child = ObjectsToMove[0].transform.GetChild(index);
 
                 distanceTraveled[index] += Vector3.Distance(child.transform.position, lastPositions[index]);
                 if (distanceTraveled[index] >= randomNumbers[index] && !hasRotated[index])
                 {
                     targetAngles = child.transform.eulerAngles + 180f * transform.up;
                     StartCoroutine(TurnShip(child.transform, child.transform.eulerAngles, targetAngles, smooth));
                     hasRotated[index] = true;
                     test = false;
                     if (test == false)
                     {
                         distanceTraveled[index] = 0;
                         lastPositions[index] = new Vector3(transform.position.x, transform.position.y, transform.position.z);
                         hasRotated[index] = false;
                         test = true;
                     }
 
                 }
             }
         }
     }
 
     IEnumerator TurnShip(Transform ship, Vector3 startAngle, Vector3 endAngle, float smooth)
     {
         float lerpSpeed = 0;
 
         while (lerpSpeed < 1)
         {
             ship.eulerAngles = Vector3.Lerp(startAngle, endAngle, lerpSpeed);
             lerpSpeed += Time.deltaTime * smooth;
             yield return null;
         }
     }
 }
 
Comment
Add comment · Show 4
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 saschandroid · Oct 18, 2016 at 06:43 AM 0
Share
 CloneSpaceShips ss = new CloneSpaceShips ();

Don't you get the warning You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. with this?

avatar image Chocolade saschandroid · Oct 18, 2016 at 07:58 AM 0
Share

Right i see the warning. So what other way/s i have to pass the spaceships to the other script without get; set; at all ? And how do i get it in the second script ? I couldn't find how to do it.

What i want is to use the spaceships array from the first script in the second script.

avatar image saschandroid Chocolade · Oct 18, 2016 at 08:10 AM 0
Share

If both scripts are on the same gameobject you can use

 CloneSpaceShips ss = GetComponent<CloneSpaceShips>();

Show more comments

1 Reply

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

Answer by b1gry4n · Oct 18, 2016 at 06:31 AM

If I understand correctly, your issue is that a script you want called second is actually being called first. One way to avoid this is to have the second script not have a void Start, but instead have a function the first script calls when youre ready to run the second. A manually controlled "start". The other way is to change the script execution order: https://docs.unity3d.com/Manual/class-ScriptExecution.html

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How can i check and fire an event when the user look at specific object ? 0 Answers

Need help fixing my script 1 Answer

How do I make a photography function? 0 Answers

How do I make my projectile deal damage to a target it hit. 1 Answer

Quick code help! (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