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 question was closed Jul 31, 2014 at 06:31 AM by RedDevil for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by RedDevil · Jul 30, 2014 at 12:30 PM · instantiategetcomponentfind

Instantiated Prefab using its transform and object on other scripts

So my problem is that on my other object i cannot select a gameobject to be the object selected in another script: i checked the other script and the problem is here:

     void Start () 
     {
         gameManager GM = GameManagerObj.GetComponent<gameManager>();
         PlayerObj = GM.PlayerStats; 
         stats = PlayerObj.GetComponent<PlayerStats>();
     }

It does not assign PlayerObj with the values of the PlayerStats found in gameManager script.

Comment
Add comment · Show 20
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 RedDevil · Jul 30, 2014 at 02:02 PM 0
Share

ins$$anonymous$$d of trying that I also tryed this:

 PlayerObj = GameObject.FindGameObjectWithTag("Player");
         stats = PlayerObj.GetComponent<PlayerStats>();

I got same error of null reference on PlayerObj.

avatar image RedDevil · Jul 30, 2014 at 02:12 PM 0
Share

also by trying to find it by name returns the same null reference error: PlayerObj = GameObject.Find("SorcererCls(Clone)"); stats = PlayerObj.GetComponent();

avatar image Arcadewiz · Jul 31, 2014 at 06:12 AM 1
Share

I can't think of anything else if your gameobject is in your hierarchy and not being dynamically created when the script tries to find it or perhaps after creating the tag you forgot to set it on the gameobject. try finding the gameobject by gameobject.find just to see if its being referenced ...

avatar image Arcadewiz · Jul 31, 2014 at 06:17 AM 1
Share

If the object is dynamically created make sure that you are trying to access it with gameobject.findwithtag after it is being created in the hierarchy. I dont know your code structure and how your script sequence work. So follow your logic and enable the code after the instance of its creation ...

avatar image Arcadewiz · Jul 31, 2014 at 06:27 AM 1
Share

Aren't you instantiating your enemies for spawning them ? when they are being spawned you can trigger this piece of code for finding with tag

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by dbrizov · Jul 30, 2014 at 12:57 PM

If you want to use the GameManager in the second script and take a reference to it in the Start() method, you need to be sure that every single reference in the GameManager script is instantiated. You should ask yourself: "Is the GameManager script ready to use? If so, then you should take a reference to it". A solution is to Instantiate the GameManager in it's Awake() method, and take it as a component in the Start() method in the second script.

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 RedDevil · Jul 30, 2014 at 01:12 PM 0
Share

if i use getcomponent on start i cant use the statsscript.exp

avatar image RedDevil · Jul 30, 2014 at 01:22 PM 0
Share

never$$anonymous$$d i forgot to make that public and put it on top but still this says i have an unasigned reference here :

 GameObject InstantiatePlayer = Instantiate(PlayerPrefab,SpawnPosition.transform.position,SpawnPosition.transform.rotation) as GameObject;
         PlayerTrans = InstantiatePlayer.gameObject.transform;
         PlayerStats = InstantiatePlayer.gameObject;
avatar image RedDevil · Jul 30, 2014 at 01:22 PM 0
Share

on PlayerStats it says its not assigned

avatar image RedDevil · Jul 30, 2014 at 01:38 PM 0
Share

i just seen that in the inspector on game$$anonymous$$anager script PlayerStats did select the right object but it still says error: here that it was not assigned void Start () { Health = 100; game$$anonymous$$anager G$$anonymous$$ = Game$$anonymous$$anagerObj.GetComponent(); PlayerObj = G$$anonymous$$.PlayerStats.gameObject; stats = PlayerObj.GetComponent(); }

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Limiting respawns on scene 1 Answer

Photon instantiate (Assign Instantiated . get component 1 Answer

Clickable GameObjects within one another... 0 Answers

After launching game on Windows x32 the script working with MySQL database could not be instantiated 0 Answers

how would I make an array of different scripts 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