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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by Lisk · Apr 10, 2015 at 10:02 PM · componentinstantiation

Modify property of an instantiated object

I'm creating a simple projectile system.

         GameObject g = Instantiate (projectilePrefab, spawnPos, faceRotation) as GameObject;
         GameProjectile proj = g.GetComponent<GameProjectile> ();
         proj.damage = 30f;


Then in the GameProjectile code, I have:

 public float damage;
 
 void Update() {
     print ("damage = " + damage);
 }

However, this keeps printing out 0, instead of 30.

How can I fix my code such that it will print out 30?

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
1

Answer by sykodev · Apr 11, 2015 at 03:20 PM

For simplicity, you could do:

 GameProjectile projectile = Instantiate (projectilePrefab, spawnPos, faceRotation) as GameProjectile;
 p.damage = 30f;

As far as your damage not being set... it IS getting set, but you may have another script overriding that value, or a typo on a different line of code where it's used. I suggest checking everywhere you use the "damage" float and making sure things are as they should be.

Comment
Add comment · 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
0

Answer by KalleH · Apr 11, 2015 at 11:56 AM

Your script should work just well and I even tried it out without any problems. Make sure that every class name is spelled correctly and that you don't have an active instance of GameProjectile already in scene view (if you do, it will print 0 since you never change its damage, only the instantiated versions'). Also make sure that the GameProjectile script is attached to the root gameobject and not any child or else you will have to edit your code a bit.

If you are still having trouble getting it work you could create a public variable proj for GameProjectile in the first script instead of using a temporary one. This allows you to see in editor that the script finds the GameProjectile component correctly. I highly believe that the reason for your script failing is related to finding that component.

If the damage is always the same you could also just simply add damage = 30 to void Start() function in your GameProjectile. I also recommend not changing variables of other scripts directly from another script and instead create a function in GameProjectile like public void setDamage(float value) which you can simply call from another script.

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 Lisk · Apr 11, 2015 at 04:55 PM 1
Share

For some odd reason, if I substitute this line:

  GameObject g = Instantiate (projectilePrefab, spawnPos, faceRotation) as GameObject;

with this:

  Transform g = Instantiate (projectilePrefab, spawnPos, faceRotation) as Transform ;

It works. Basically, the same line, but ins$$anonymous$$d of GameObject, I use a Transform. Weird, isn't it?

And good call--accessors & getters is good practice, and that's actually my intention, but I wanted to simplify the code as much as possible for getting the question answered first. :)

avatar image KalleH · Apr 11, 2015 at 09:17 PM 0
Share

That sounds pretty weird indeed, I have never seen anything like that happen. What does your prefab consist of? Gameobject with a script and a collider attached to it?

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

20 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

Related Questions

Caching Transform Javascript Help 0 Answers

Instantiate prefab wihtout dragging prefab into gameObject script 9 Answers

2D Animation does not start 1 Answer

Select all game objects with a component 1 Answer

Disabling A Script on a GameObject From a Different Script 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