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 BenBonkGames · May 05, 2019 at 11:19 PM · scripting problemgameobjectinstantiate

Add Component with Script Data

Hi, so I have a script like this,

  public void Player1()
     {
         var go = Instantiate(p1, new Vector2(-0.5f, -5), Quaternion.identity)as GameObject;
         go.AddComponent<PlayerMove>();
         go.AddComponent<PlayerAttack>();       
         DontDestroyOnLoad(p1);
         sm.NextCharacterSelect();
         
         }

         

And I am trying to add a script to an instantiated game object but whenever I add the script it doesn't have any data that I already assigned in the inspector, for example changing the move speed to 10 and jump force to 5. So what I am asking is if there is any way I can add a script to an object but already have the stuff you need to assign in the inspector predetermined. I'm not sure if this makes sense but any help would be nice.

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
0

Answer by Hellium · May 06, 2019 at 05:08 AM

When a component is attached (from editor or by script), the assigned values are the ones you out when declaring the variables or the ones you assign in the Awake / Start method.

 public int Lives = 5;
 public float Score; // default value = 0
 public string Name; // default value = null

 private void Awake()
 {
      Name = "unknown";
 }

If you want to change the values of the variable when adding the component, either change them here, or assign them one by the one from the calling script or create a function to easily change them all.

 public void Init( int lives, float score, string name )
 {
     Lives = lives;
     Score = score;
     Name = name;
 }

 // In your other script

 PlayerAttack attack = go.AddComponent<PlayerAttack>();
 attack.Init( 10, 0, "John Doe");
Comment
Add comment · Show 4 · 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 BenBonkGames · May 06, 2019 at 10:01 PM 0
Share

Hello thanks for the response, I assigned the float and int values for my character movement in my script, and it works fine, however, I was wondering if there was a way to Automatically know my public Transform's or Scripts that I am accessing from somewhere else. Sorry if you already mentioned the answer I'm kind of new to Unity and I don't fully understand everything.

avatar image Hellium BenBonkGames · May 07, 2019 at 05:11 AM 0
Share

I don't really understand your question:

I was wondering if there was a way to Automatically know my public Transform's or Scripts that I am accessing from somewhere else

Could you provide an example and/or drawing?

avatar image BenBonkGames · May 07, 2019 at 10:34 AM 0
Share

Hi I was kind of talking about this sort of stuffalt text

alt text

cafdspture.png (5.6 kB)
capturfde.png (3.6 kB)
avatar image Hellium BenBonkGames · May 07, 2019 at 11:13 AM 0
Share

I am sorry, I still don't understand everything.

$$anonymous$$aybe the several "solutions" betlow is what you want

Supposing you have a Spawner script responsible for instantiating your players. The Player can have a reference to the spawner like this:

      var go = Instantiate(p1, new Vector2(-0.5f, -5), Quaternion.identity)as GameObject;     
      PlayerScript player = go.AddComponent<PlayerScript>();
      player.Spawner = this; // this is a keyword meaning the current instance running the code


If you want your player to be aware of the Scene$$anonymous$$anager, referenced by the spawner, the logic is the same

     public Scene$$anonymous$$anager sm;

     // ...

      var go = Instantiate(p1, new Vector2(-0.5f, -5), Quaternion.identity)as GameObject;     
      PlayerScript player = go.AddComponent<PlayerScript>();
      player.Scene$$anonymous$$anager = sm;

If you want your player to get a reference to another object, not referenced by its spawner, you can use the FindXXX function according to your needs

  // In the player script
  public void Start()
  {
      SomeGameObject = FindObjectOfType<ScriptName>();
  }

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

289 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 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

RPG instantiate problem 3 Answers

Instantiate() and Destroy() vs setActive() 1 Answer

Issue with Prefab instantiate and script not working? 0 Answers

How to instantiate a game object every time when space key is pressed 2 Answers

How to getcomponent on an object instantiated through PUN 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