Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 G3R1 · Apr 09, 2017 at 11:35 AM · scenesspawnpointsdifferent

Different spawn points from different scenes ? [Solved]

Hi guys,

Let's say I have 4 scenes. The first scene contains only one empty gameobject that has another empty gameobject as a child. The parent gameobject has a script using the function DontDestroyOnLoad() so when the other scene loads it will not be destroyed. The child gameobject uses a script to spawn objects at different positions and it uses public Transform[] spawnPositions to get the positions.

The other 3 scenes have one parent gameobject which is empty and contains this script that uses public Transform[] positions; . Also the parent contains 3 empty gameobject that are basically just the positions that i need to access. So one of the scenes contains three positions, the other one three positions and the last one three positons too.

Now I just want to access the positions on each scene and pass it to the script that spawns the object. So when the spawn script is loaded on one of the scenes it uses the positions of that scene and so on.

I have searched a lot and didn't find a proper solution. I also have tried dozens of ways to achieve it and no success.

Do you have any idea how i can accomplish this ?

Thank you. :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cuttlas-U · Apr 09, 2017 at 04:40 PM

Hi; as u said your main script wont destroy and its a good idea; so the script that should get the new positions show be this script; other scripts in different scenes just hold the transforms and they should not do any thing ;

first u have to do this on " OnLevelWasLoaded " function ;

this happens when u load a new scene ; then u can search for the right object and get the component and positions ;

for example if the component that hold your positions in the scene name is " PositionHolder" and it has a array named "position" , and the array in your main script is named "AllPositionsInScene" , then u should do it like this ;

  void OnLevelWasLoaded()
     {
 
         for (int i = 0; i < AllPositionsInScene.Length; i++)
         {
             AllPositionsInScene[i] = GameObject.FindObjectOfType<PositionHolder>().position[i];
         }
 
     }

this will override the previous positions and set them to new ones that are in the loaded scene;

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 G3R1 · Apr 09, 2017 at 05:13 PM 0
Share

Hi, thank you for your response. The thing is that the "PositionHolder" is not located in the scene where the main script is not destroyed but at the other scenes that hold the positions where the object are going to be spawned. When I wrote this function as you said it gave me an error "The type or namespace 'PositonHolder' could not be found. Are you missing an assembly reference?"

I am explaining it again in case it helps you understand better whats going on.

The spawn script contains this code:

 public class Instantiate : $$anonymous$$onoBehaviour {
 
 //Here spawnPositions used to get assigned with drag and drop on inspector using three gameobjects but on the same scene as this script and it worked fine...
 public Transform[] spawnPositions;
 
 //I am using an array here so to assign different objects to spawn
 public Rigidbody[] objectPrefab;
 
 Rigidbody object1;
 Rigidbody object2;
 
 //Replaced this the function exactly as you said....
 void OnLevelWasLoaded()
     {
         for (int i = 0; i < spawnPositions.Length; i++)
         {
             spawnPositions [i] = GameObject.FindObjectOfType<PositionHolder> ().position [i];
         }
     }
 
 void Update () {
 
 //random index inside spawnPositions is generated in another function below which i didn't include on this code...it works and its not needed...
 object1 = Instantiate (objectPrefab [0], spawnPositions [random].position, Quaternion.Euler(randomRot, randomRot, randomRot)) as Rigidbody;
 
 object2 = Instantiate (objectPrefab [1], spawnPositions [random].position, Quaternion.Euler(randomRot, randomRot, randomRot)) as Rigidbody;
 
 }
 }

The script above is attached to the child gameobject of the main script that doesn't destroy on load ( which is attached to the parent gameobject).

Now at the other scenes i have a hierarchy of gameobjects like this: 1- PositionHolder(a script is attached in this gameobject and inside script is only this code : "public Transform[] positions;" 1.1 - Position1 (child gameobject of 1), 1.2- Position2 (child gameobject of 1), 1.3 - Position3 (child gameobject of 1),

In case you didn't undertand something pls tell me. I have been trying a lot of other codes today and none of them has worked. Have no idea why. Thank you.

avatar image
0

Answer by G3R1 · Apr 09, 2017 at 09:47 PM

The problem seems to be something else which i haven't solved yet. In the main script i have the codes that make the 'gamecontrol' object to load on other scenes without getting destroyed. In this gamecontrol i have just declared a public static GameControl control;

Now, I erased all the codes i was trying to fix and made an experiment to know where the problem was. I created a UI text in order to display some information from the spawn class that loads on each scene without being destroyed. I used these codes :

 public Text text;
 
 void Start()
 {
 text.text = GameControl.control.GetComponent<SpawnScript>().spawnPositions.Length.ToString();
 
 }

The error I got is the same that was displayed before: "NullReferenceException: Object reference not set to an instance of an object System.Single.ToString () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Single.cs:241)"

Maybe there is some strange going on with the static variable ?

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 G3R1 · Apr 09, 2017 at 10:46 PM 0
Share

Guys I finally found the problem even though i don't understand it why did it happen. The problem was with the spawn script attached to the child gameobject of the main script gameobject. The spawn script should be attached to the main gameobject and not the child so the static variable applies the reference to Transform[] array, otherwise it will not pass those values from on array to the other. It will keep saying null reference. $$anonymous$$aybe there is a good explanation about this but I am not capable to find out why for the moment. You can try the scenario above in case you want to find the real exaplanation by yourslef. Thank you for the time.

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

66 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

Related Questions

Respawn player at different Spawnpoints 1 Answer

Send data through network between different objects/scenes 1 Answer

How do I refer to a .text from a separate scene. 0 Answers

accesing objects variables 1 Answer

Different behaviour when loading a scene for the second time 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