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
0
Question by tt0022 · Oct 27, 2015 at 12:02 PM · c#unity 5instantiate

how do i remember a just instantiated prefab as a variable gameobject?

i want to assign a gameobject to my just instantiated prefab. the problem is. it needs to change depending on what lane the prefab spawned on. (each prefab needs to remember the object that spawned the prefab) i have tried this:

 GameObject MonsterInstance = Instantiate(PrefabGreenMonster, SpawnPoint.position, SpawnPoint.rotation) as GameObject;
 MonsterInstance.GetComponent<WalkingScript>().Target_LaneController = gameObject;

but sadly, for some reason i cant get it to work.

i need to find a way to send something to the instantiated prefab.

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 NoseKills · Oct 27, 2015 at 04:34 PM 0
Share

What does "can't get it to work" mean? An error? A crash? You can play the game but it doesn't work as intended?

You are pretty much just assigning a value to a variable, so the first thing that comes to $$anonymous$$d is that maybe the types don't match.

Is WalkingScript.Target_LaneController of type GameObject? It needs to be since you are trying to assign a GameObject into it

avatar image tt0022 NoseKills · Oct 27, 2015 at 04:54 PM 0
Share

the Target_LaneController is a variable in the other script. it is meant to link back to the lane controller script (the one with this code, and that will spawn the prefab). also WalkingScript is the target script, i need access to that script so i can link it to the lane controller.

basically, script_A needs to instantiate object_b with script_B. but when instantiating, it also needs to link script_B back to script_A, so that Script_B can read out some variables in script_A.

what i could not get to work, is any kind of connection to the script/gameobject i just instantiated. when instantiating, i can give it a location, add force to it so it shoots like a bullet. but i can not get a simple thing like any other kind of variable to the prefab.

1 Reply

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

Answer by ElDo · Oct 27, 2015 at 06:52 PM

Let's see if I got you right on this.

you could have your Script_A like this:

 public class Script_A : NetworkBehaviour {
 public int number=5;
 void Start(){
  GameObject o = Instantiate(Prefab,Position,Rotation) as GameObject;
  o.GetComponent<Script_B>().scriptA=this;
 }
 }

Now your Script_B Needs a global(public) Variable of Type Script_A (in my example it has the Name "scriptA" so Script_A can link itself (with "this") to Script_B. Now after the new GameObject with Script_B attached has been spawned it can Access all global (public) Variables of Script_A:

 public class Script_B : NetworkBehaviour{
  public Script_A scriptA;
  void Start(){
   Debug.Log(scriptA.number);
  }
 }

Now your Script_B should print out the number 5 of Script_A

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 tt0022 · Oct 27, 2015 at 07:44 PM 0
Share

do i need to use NetworkBehaviour ins$$anonymous$$d of $$anonymous$$onoBehaviour for this to work?

i have re created this, but for some reason my script_B does not receive the connection to script_A. public Script_A scriptA; always stays emptey, and Debug.Log(scriptA.number);did not debug anything, (probably because he could not find anything.)

avatar image ElDo tt0022 · Oct 27, 2015 at 07:48 PM 0
Share

it does not Need to be an NetworkBehaviour. could you copy/paste your code here so I can crosscheck it?

avatar image tt0022 ElDo · Oct 27, 2015 at 07:59 PM 0
Share

script_A

 public class LaneController : $$anonymous$$onoBehaviour {
 //test
 public int number = 5;
 void Update ()
     {
         if (SpawnTestButton == true)
         {
 GameObject $$anonymous$$onsterInstance = Instantiate(PrefabGreen$$anonymous$$onster, SpawnPoint.position, SpawnPoint.rotation) as GameObject;
                 SpawnTestButton = false;
                 $$anonymous$$onsterInstance.GetComponent<WalkingScript>().scriptA = this;
         }
 }

script_B

 public class WalkingScript : $$anonymous$$onoBehaviour {
 //test
     public LaneController scriptA;
 
     // Use this for initialization
     void Start ()
     {
         //test
         Debug.Log(scriptA.number);
 }
 }
Show more comments
Show more comments
avatar image ElDo tt0022 · Oct 27, 2015 at 09:06 PM 0
Share

Well you should not spawn a Rigidbody as a GameObject! Where do you get your null Reference Exceptions from? What line of code/Variable?

avatar image tt0022 ElDo · Oct 27, 2015 at 10:23 PM 0
Share

yea, that was a little mistake on my part. but your code did the job. but for some reason i had to change all the game objects in script_A to rigidbody's. i still don't know exactly why i get null reference when they are gameobjects, and why they disappear when they are rigidbody's. but it works now.

as i said before, thanks for the help.

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

41 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

Related Questions

Referencing the Transform of the children of an Instantiated GameObject _PLEASE HELP!!! 1 Answer

I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer

How do I Instantiate a prefab at the x axis position of where the UI button was clicked 1 Answer

Problem with Instantiate custom class 0 Answers

Problem with projectile direction 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