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 /
  • Help Room /
avatar image
0
Question by Kasumi_Gake · Nov 03, 2021 at 12:27 PM · scripting problemscript.

Problem with executing scripts

Hello,

Befor you start reading, I apologize for the length of this message. I tried to be as precise as possible on the steps I took, but since I'm a beginner, some of this might sound stupid...

So, I'm kinda new to unity and c#, and I'm practicing at programming, mostly by creating at least the start of a VG. What I do is that I randomly generate ennemies on a tileset, and 4 controllable players. Where the thing is getting tricky for me, is that I would like enemies and controlable characters to share the same set of combat variables, so to do this, I created a script for each enemy and character named "character_data.cs" with a unique namespace for each of these, and a single script named "combat_sheet.cs", that only contains the variables needed for combat. Both scripts are added to the characters and enemy prefabs, and in the start() of the script "character_data", for each variables, I do :

start(){ this.GameObject.GetComponent().variable_in_combat_sheet = variabe_in_character_data ; }

Since all my prefabs are generated using : Instantiate(prefab) as GameObject; I would expect all instantiated prefabs getting an independant copy of the "Combat_Sheet.cs" script. Obviously, this is not the behavior I observe :

I made a list of all the fighters on the tileset, that I sort following a variable called "next_turn", then I have 3 coroutines : -one that just wait for the space key to be pressed (to simulate player turn) -one that deals with turn for the enemies -A global one that alternates bewteen the other 2, depending on wether the first fighter in the list is a player or an enemy.

At the end of every turn, I do :

selected_fighter.GetComponent().next_turn += 25;

And then I sort the fighters again. It didn't seem to work, so at the sorting step, for all elements in the list, I do :

Debug.Log(fighter.GetComponent().next_turn);

So I always get to player's turn upon starting the game, where I'm expected to press Space, and when I do so, I see that the next_turn variables don't really work well : most of the time, all fighter's next_turn variable will change at the same time on pressing space, the values are different from what is shown on the scripts in the inspector window, and when I stop and restart the game, the values don't seem to have been reseted. Values are set back to 0 when reopening the project, although these are not supposed to be 0 (more like 20-25);

At some point, I tried removing the Combat_Sheet component from the prefabs and adding them at runtime, but then the sorting function would throw "Object reference not set to an instance of an object" exceptions.

I guess I'm doing something terribly and stupidly wrong, but I can't figure out what. Do you have any hint of what could be going wrong? Thanks KG

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 Kasumi_Gake · Nov 03, 2021 at 02:03 PM

Ok, I knew it was something stupid I found it : The function I use to instantiate the fighters returned the prefab and not the game object created... Sorry for the inconveniance, KG

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

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

291 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 avatar image avatar image

Related Questions

Script to random set active a child object? 0 Answers

How to make an object move between a certain range and have that range to be taken into account not with a transform.position value but with the object's dimensions? 1 Answer

How to make health/damage system on collision? 0 Answers

Swipe Detection Script not working as intended 0 Answers

error CS1525: Unexpected symbol `(', expecting `,', `;', or `=' 1 Answer


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