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
2
Question by ChefZweegie · Nov 19, 2011 at 12:02 AM · instantiatevariables

Passing variables to an instantiated object

Dear Community,

I wanted to see if there was a good solution for passing variables to an object that is instantiated. I am creating bullets this way and need to be able to pass in variables to the bullet class which define the direction, type of bullet, and so on based on the current status of the player (two player game). Here is a sample of the code I am using:

using UnityEngine; using System.Collections;

public class PlayerFire : MonoBehaviour { public GameObject bullet; // must be assigned in inspector private string playerFire;
private PlayerInfo playerInfo;

 // Use this for initialization
 void Start () {
 
     playerInfo = GetComponent(typeof(PlayerInfo)) as PlayerInfo;
     playerFire = playerInfo.fire;
     
     bullet.player = "player1";
 }
 
 // Update is called once per frame
 void Update () {
     
     if (Input.GetButtonDown(playerFire))
         Instantiate(bullet, transform.position, Quaternion.identity);
     
 
 }

}

using UnityEngine; using System.Collections;

public class Bullet : MonoBehaviour {

 float bulletSpeed = 15.0f; // speed of the bullet to travel
 GameObject cam; // used to ref camera object in order to destroy bullet if it has left the camera view
 Movement mover;
 public string player;
 BoxCollider colBox; // 
 Vector3 bulletPosition;
 float bulletDirectionX;
 float bulletDirectionY;
 
 // Use this for initialization
 void Start () {
             bulletPosition = new Vector3(0, 0, 0);
             mover = GameObject.Find(player).GetComponent(typeof(Movement)) as Movement;
             
             cam = GameObject.Find("Main Camera");
             colBox = GetComponent(typeof(BoxCollider)) as BoxCollider;
         
             bulletDirectionX = mover.fireDirectionX;
             bulletDirectionY = mover.fireDirectionY;
             
     }
 
 // Update is called once per frame
 void Update () {
     //move bullet
     //check for collision
     //check for leaving view
     bulletPosition.x = bulletDirectionX*Time.deltaTime*bulletSpeed;
     bulletPosition.y = bulletDirectionY*Time.deltaTime*bulletSpeed;
     colBox.transform.Translate(bulletPosition);
     
     
 }
 
 void OnBecameInvisible ()
 {
     Destroy(gameObject);
 }
 
 void bulletDestroy() 
 {
     Destroy(gameObject);
     
 }    
 void OnCollisionEnter(Collision objHit)
 {
     if(objHit.gameObject.tag == "enemy" ){
     Destroy(objHit.gameObject);
 }
 }
 

}

The final solution that I want to avoid is creating two bullet classes so that each player has their own and it pulls from that.

Zweegie

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

1 Reply

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

Answer by DaveA · Nov 19, 2011 at 12:52 AM

Instantiate returns the created object:

 var newGO = Instantiate (......

So you can use GetComponent on newGO to get/set such things

Comment
Add comment · Show 2 · 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 ChefZweegie · Nov 19, 2011 at 04:45 AM 0
Share

Awesome! That was the solution. I had a $$anonymous$$or problem when I made this change. I got some nulls, but forgot that my player scripts needed to be updated when I made a few changes to the bullet object. I ended up going with your solution, but changed the GameObject to type Bullet and then had to drag and drop into a few different locations. The variables pass through nicely. Changed my code to this:

using UnityEngine; using System.Collections;

public class PlayerFire : $$anonymous$$onoBehaviour { public Bullet bullet; // must be assigned in inspector private string playerFire; private PlayerInfo playerInfo;
private int playerNumber; // Use this for initialization void Start () {

     playerInfo = GetComponent(typeof(PlayerInfo)) as PlayerInfo;
     playerFire = playerInfo.fire;
     playerNumber = playerInfo.playerNumber;
     
 }
 
 // Update is called once per frame
 void Update () {
     
     if (Input.GetButtonDown(playerFire))
         createBullet();
     
 }
 
 void createBullet()
 {
     Bullet zBullet = Instantiate(bullet, transform.position, Quaternion.identity) as Bullet;
     zBullet.playerNum = playerNumber;
 }
 
 

avatar image MonkeyPuzzle · Nov 02, 2019 at 01:20 AM 0
Share

This answer worked for me. thx.

 var newGO = Instantiate (...);
 newGO.GetComponent<script>().publicVariable=newVal;

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Adding a variable to instantiated object causes infinite objects to spawn. 2 Answers

Checking if object intersects? 1 Answer

How to create a object that can only be seen by one player? 0 Answers

Variable not Assigned 1 Answer

Instantiate Code Issue 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