Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DuxFortis · Jan 04, 2021 at 08:17 PM · instantiatevector3variabletransform.position

How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab

Hi!

Been stuck for a while figuring out how to fix this problem. Hoping for any help at all!

 public class PlaceNewBaseButton : MonoBehaviour
 {
     public GameObject baseMenu;
     public GameObject GeoscapeUi;
     public Button buildNewBase;
     public Button cancelBuild;
 
 
 
 
     public bool HasBaseBeenBuilt = false;
     public bool BaseSuccessfullyBuilt = false;
 
     public GameObject baseBox;
 
     public spawnFighter spawner;
 
     public Vector3 baseBoxPos;
 
     public float baseCountCheck = 0;
     public void Start()
     {
     }
 
     public void Update()
     {
         
 
         if (baseCountCheck == 0)
         {
             buildBase();
         }
 
         if(baseCountCheck == 0 && HasBaseBeenBuilt == true)
         {
             CancelInvoke("buildBase");
             Debug.Log("double call bug, look for the script attached to other gameobjects");
         }
 
         void buildBase()
         {
 
           
 
             if (baseCountCheck == 8)
             {
                 Debug.Log("You have exceeded the base limit");
                 cancelledBuild();
             }
 
 
 
 
             Time.timeScale = 0f;
             Debug.Log("Waiting for base to be built");
             if (Input.GetMouseButtonDown(0))
             {
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 RaycastHit hit;
                 if (Physics.Raycast(ray, out hit))
                 {
                    GameObject Base = (GameObject)Instantiate(baseBox, hit.point, Quaternion.identity);
                     Base.transform.position = hit.point;
                     baseBoxPos = Base.transform.position;
 
 
                     print(Base.transform.position);
                     baseCountCheck += 1;
                     baseBuilt();
 
 
                 }
 
             }
         }
         void baseBuilt()
         {
 
             HasBaseBeenBuilt = true;
             Time.timeScale = 1f;
             Debug.Log("Base built");
             GeoscapeUi.SetActive(true);
             
             baseMenu.SetActive(false);
         }
 
         void cancelledBuild()
         {
             Time.timeScale = 1f;
             Debug.Log("Canceled");
             HasBaseBeenBuilt = false;
             baseMenu.SetActive(true);
 
         }
 
         cancelBuild.onClick.AddListener(cancelledBuild);
         buildNewBase.onClick.AddListener(buildBase);
 
 
     }
 
 }
 

The variable in question is the baseBoxPos, In my spawning script, i use this to get the position to spawn, but whenever a base is placed, the value in the spawning script goes null, so the spawn will always happen at 0,0,0.

 public class spawnFighter : MonoBehaviour
 {
 
     public GameObject interceptor;
     public GameObject baseBox;
     public PlaceNewBaseButton baseInfo;
 
 
     private float interceptorspawnCounter = 0f;
     private float maxinterceptorspawnLimit = 2f;
 
     public Vector3 posOfBase;
 
     // Start is called before the first frame update
    public void Start()
     {
         posOfBase = GetComponent<PlaceNewBaseButton>().baseBoxPos;
 
         if (posOfBase == null)
         {
             Debug.Log(posOfBase);
         }
 
 
     }
 
    
 
 
 
     public void SpawnObject()
     {
         if(baseBox.activeSelf == false)
         {
             CancelInvoke("SpawnObject");
         }
 
         if(interceptorspawnCounter >= maxinterceptorspawnLimit)
         {
             interceptorspawnCounter = 0f;
             maxinterceptorspawnLimit = 2f;
         }
 
         if(interceptorspawnCounter <= maxinterceptorspawnLimit)
         {
             Debug.Log(posOfBase);
            Instantiate(interceptor,posOfBase,Quaternion.identity);
             
             
 
             interceptorspawnCounter++;
             
 
         }
         else
         {
             Debug.Log("You can only send two interceptors at a time");
         }
 
 
 
 
 
 
 
     }
 }



 

I am completely stuck on this, I don't know how to move forward, any help at all would be greatly appreciated!

Comment
Add comment · Show 2
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 Llama_w_2Ls · Jan 04, 2021 at 08:26 PM 1
Share

You're trying to get the value of baseBoxPos in the Start() method, which runs before the value of baseBoxPos is set in the Update() method. You would need to retrieve it at a later point, such as after Input.Get$$anonymous$$ouseButtonDown(0), when it is set in the first script. @DuxFortis

avatar image DuxFortis Llama_w_2Ls · Jan 04, 2021 at 08:40 PM 0
Share

Thank you! I'll try my hand at doing that.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Jan 04, 2021 at 08:38 PM

It looks like in your second script you are grabbing the position in your Start method, but in your first script you aren't actually setting that position until some later time. So when your second script grabs the value its equal to 0, 0, 0, and when that value is updated later your second script does not see that update. Instead you should make sure you grab the base box pos when you actually need it. Try something like this.

 public PlaceNewBaseButton baseButton;
 
 // Start is called before the first frame update
 public void Start () {
     baseButton = GetComponent<PlaceNewBaseButton> ();
 
 }
 
 public void SpawnObject () {
     //blah blah blah
     Vector3 posOfBase = baseButton.baseBoxPos;
     if (interceptorspawnCounter <= maxinterceptorspawnLimit) {
         Debug.Log (posOfBase);
         Instantiate (interceptor, posOfBase, Quaternion.identity);
     }
 }

The issue is that Vector3 is a struct, not a class, so when you say something like posOfBase = GetComponent<PlaceNewBaseButton>().baseBoxPos; you are making a copy of the value of baseBoxPos but you are not getting a reference to the data itself. This means that if the original data changes its value, your copy of the data will not.

Comment
Add comment · Show 11 · 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 DuxFortis · Jan 05, 2021 at 11:12 AM 0
Share

Hey, I stopped getting the Null exception error on making the baseBox object, and now only get it during the click event to spawn an interceptor! I think it has something to do with the PlaceNewBaseButton script. Thanks for the help so far!

avatar image unity_ek98vnTRplGj8Q DuxFortis · Jan 05, 2021 at 03:47 PM 0
Share

Can you paste your new code and tell me exactly which line is throwing the null ref exception?

avatar image DuxFortis unity_ek98vnTRplGj8Q · Jan 05, 2021 at 05:18 PM 0
Share

Sure! it's line 32 which is the declared local variable posOfBase right at the start of SpawnObject

 

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class spawnFighter : $$anonymous$$onoBehaviour
 {
 
     public GameObject interceptor;
     public GameObject baseBox;
     public PlaceNewBaseButton baseButton;
     
 
 
     private float interceptorspawnCounter = 0f;
     private float maxinterceptorspawnLimit = 2f;
 
     
 
     // Start is called before the first frame update
     public void Start()
     {
         baseButton = GetComponent<PlaceNewBaseButton>();
         
     }
 
 
 
     
 
     public void SpawnObject()
     {
         Vector3 posOfBase = baseButton.baseBoxPos;
 
         if (interceptorspawnCounter <= maxinterceptorspawnLimit)
         {
             Debug.Log(posOfBase);
            Instantiate(interceptor,posOfBase,Quaternion.identity);
             
             
 
             interceptorspawnCounter++;
             
 
         }
         else
         {
             Debug.Log("You can only send two interceptors at a time");
         }
 
 
 
 
 
 
 
     }
 
 
 
 }
 
Show more comments
avatar image
0

Answer by tkati · Jan 06, 2021 at 04:40 PM

shouldn't the code be more like baseButton.transform.position ?

edit: at this line Vector3 posOfBase = baseButton.baseBoxPos;

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 DuxFortis · Jan 06, 2021 at 04:51 PM 0
Share

Tried it, nothing changed, still get the same null exception error on that line...

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

163 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

Related Questions

Instantiate object in front of player? 2 Answers

transform.position on Instaniated object does not match inspector 0 Answers

Updating transform.position correctly to instantiated GameObjects 0 Answers

How could I make a border around my grid world? (PIC INCLUDED) 1 Answer

The instantiated prefab doesn't fire correctly from the player 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